通过设计错误管理用户

时间:2015-07-23 22:56:30

标签: ruby-on-rails devise controller

我已经在我的代码下面设置了一个设计的Crud接口,我遇到的问题是,当我尝试创建用户时,得到以下错误。我也无法编辑。请帮忙。

“2个错误禁止此用户被保存: 电子邮件不能为空 密码不能为空“ 当然,我一直在输入所有包含电子邮件和密码的信息。我按照设计的文档阅读并阅读了本网站的一些设计主题没有报告此类问题。     的控制器:     class UsersController< ApplicationController的     before_filter:authenticate_user!     before_action:authorized_user     #GET / users     #GET /users.json     def指数       @users = User.all

  # respond_to do |format|
  #   format.html index.html.erb
  #   format.json { render :json => @users }
  end


# GET /users/1
# GET /users/1.json
def show
  @user = User.find(params[:id])

  # respond_to do |format|
  #   format.html show.html.erb
  #   format.json { render :json => @user }
  end

# GET /users/new
# GET /users/new.json
def new
  @user = User.new

  # respond_to do |format|
  #   format.html new.html.erb
  #   format.json { render :json => @user }
  end

# GET /users/1/edit
def edit
  @user = User.find(params[:id])
end

# POST /users
# POST /users.json
def create
  @user = User.new(params[:user_params])

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, :notice => 'User was successfully created.' }
      format.json { render :json => @user, :status => :created, :location => @user }
    else
      format.html { render :action => "new" }
      format.json { render :json => @user.errors, :status => :unprocessable_entity }
    end
  end
end

# PUT /users/1
# PUT /users/1.json
def update
  if params[:user][:password].blank?
    params[:user].delete(:password)
    params[:user].delete(:password_confirmation)
  end
  @user = User.find(params[:id])

  respond_to do |format|
    if @user.update_attributes(params[:user_params])
      format.html { redirect_to @user, :notice => 'User was successfully updated.' }
      format.json { head :ok }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @user.errors, :status => :unprocessable_entity }
    end
  end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user = User.find(params[:id])
  @user.destroy

  respond_to do |format|
    format.html { redirect_to users_url }
    format.json { head :ok }
  end
end


private


def user_params
  params.require(:user).permit(:username, :name, :lastname, :email, :password, :password_confirmation, :role_id)
end

def authorized_user
  if current_user.role.name == "admin"
  else
  redirect_to :root, notice: "Not authorized" if @current_user.role.name == 'user' or nil?
  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 |msg| %>
            <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
<% end %>

<div class="field">
  <%= f.label :username %><br />
  <%= f.text_field :username, autofocus: true %>
</div>

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name, autofocus: true %>
</div>

<div class="field">
  <%= f.label :lastname %><br />
  <%= f.text_field :lastname, autofocus: true %>
</div>

<div class="field">
  <%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
  <%= f.label :password %>
  <% if @minimum_password_length %>
      <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="field">
  <%= f.label :role_id %><br />
  <%= f.collection_select :role_id, Role.all, :id, :name_role_select  %>
</div>
<div class="actions">
  <%= f.submit "Sign up", class: 'button' %>
</div>

1 个答案:

答案 0 :(得分:1)

您未正确使用user_params

而不是在您的创建操作中:

User.new(params[:user_params])

您需要这个,它会调用控制器中定义的user_params方法:

User.new(user_params)

您原来的方式是查找从您的请求传入的名称为'user_params'的params(不存在)。

同样,对于您需要采取的update行动:

if @user.update_attributes(user_params)
  #etc...