Ruby on rails 设计:手动加密密码并添加到rails中的数据库
在rails项目中,我希望通过管理员管理我的用户,这意味着管理员可以添加新用户、更新用户名和密码以及删除用户。现在我编写下面的代码来执行此操作。通过下面的代码,我可以更新和删除一个用户,但当我想向数据库添加新用户时,designe的默认验证是正常工作的,比如密码不能为空,但当我单击submit`添加用户时,页面重定向到上一页,但用户不添加到数据库 对于管理用户,我使用Desive并创建一个userscontroller。我有以下代码: 用户\u controller.rb rootes.rb 我认为要将用户添加到数据库中,我必须加密我的密码,然后将电子邮件和加密的密码添加到数据库中。但我不知道该怎么做。有什么想法吗 服务器日志:Ruby on rails 设计:手动加密密码并添加到rails中的数据库,ruby-on-rails,devise,Ruby On Rails,Devise,在rails项目中,我希望通过管理员管理我的用户,这意味着管理员可以添加新用户、更新用户名和密码以及删除用户。现在我编写下面的代码来执行此操作。通过下面的代码,我可以更新和删除一个用户,但当我想向数据库添加新用户时,designe的默认验证是正常工作的,比如密码不能为空,但当我单击submit`添加用户时,页面重定向到上一页,但用户不添加到数据库 对于管理用户,我使用Desive并创建一个userscontroller。我有以下代码: 用户\u controller.rb rootes.rb 我
你能在日志中发布错误吗?我编辑问题并添加服务器日志。当你想从管理员添加用户并再次检查时,你能评论可注册模块以覆盖默认注册行为吗?我不验证默认可注册模块。我从模型中删除了:registerable,但我得到了错误,并将其再次添加到模型中。
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.all
end
def show
end
def new
@user = User.new
end
def edit
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :password)
end
end
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
devise_for :users
resources :users, :controller => "users"
Started GET "/users/new" for 127.0.0.1 at 2014-07-12 15:49:20 +0430
Processing by UsersController#new as HTML
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER
Rendered users/_form.html.erb (3.0ms)
Rendered users/new.html.erb within layouts/application (5.0ms)
Completed 200 OK in 76ms (Views: 22.0ms | ActiveRecord: 1.0ms)
Started POST "/users" for 127.0.0.1 at 2014-07-12 15:49:43 +0430
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"FNiCblkf5kmRxAWtoRcTvwu8LZ/Rm
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2014-07-12 15:49:43 +0430
Processing by HomeController#index as HTML
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 5ms (ActiveRecord: 0.0ms)
Started GET "/dashboard" for 127.0.0.1 at 2014-07-12 15:49:43 +0430
Processing by DashboardController#index as HTML
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER
Rendered dashboard/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 21ms (Views: 17.0ms | ActiveRecord: 1.0ms)