我正在使用Rails 4构建一个简单的Admin界面来添加和删除用户,或CRUD。
我目前正在安装Devise并构建了基本的视图和操作。
然而,在自己填写表格后,这就是我得到的:
2个错误禁止此用户被保存:
电子邮件不能为空白
密码不能为空
这就是我目前的情况:(由于我不想公开注册,因此删除了可注册的内容)
模型> user.rb
class User < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end
观看&gt;用户&gt; new.html.erb
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Create User" %>
</div>
<% end %>
routes.rb (目前我通过“/ admin / users / new”创建新用户)
Rails.application.routes.draw do
devise_for :users
scope '/admin' do
resources :users
end
end
控制器&gt; users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def edit
@user = User.find(params[:id])
end
def create
@user = User.new(params[user_params])
if @user.save
redirect_to @user, :flash => { :success => 'User was successfully created.' }
else
render :action => 'new'
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[user_params])
sign_in(@user, :bypass => true) if @user == current_user
redirect_to @user, :flash => { :success => 'User was successfully updated.' }
else
render :action => 'edit'
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path, :flash => { :success => 'User was successfully deleted.' }
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
有没有人对导致错误的原因有任何想法? 谢谢:D
请注意:之前,在我禁用之前:在user.rb中的devise_modules中注册,我实际上可以使用默认设计视图“users / sign_up”执行注册。但是我想要的仍然是获得一个管理CRUD系统,所以无论如何我都禁用了它。
答案 0 :(得分:2)
是def create/update
@user = User.new(params[user_params])
# should be
@user = User.new(user_params)
if @user.update_attributes(params[user_params])
# should be
if @user.update_attributes(user_params)