在rails项目中,我想通过管理员管理我的用户,这意味着管理员可以add new user
,update username and password
和delete a user
。现在我写下面的代码来执行此操作。通过以下代码,我可以update
和delete
用户,但是当我需要add new user to database
时,设计的默认验证工作正常,(例如password can't blank), but when I click on
submit`添加用户,页面重定向到上一页,但用户不会添加到数据库。
对于管理用户,我使用devise
并创建userscontroller
。我有下面的代码:
users_controller.rb
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
app/views/users/new.html.erb
<%= 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 %>
rootes.rb
devise_for :users
resources :users, :controller => "users"
我认为要将用户添加到数据库,我必须encrypted my password
然后将email
和encrypted_password
添加到数据库。但我不知道我该怎么做。有什么想法吗?
server log
:
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)