【发布时间】:2016-02-21 17:28:31
【问题描述】:
我关注 this tutorial 进行 cancancan 身份验证,特别希望管理员无需密码即可更新用户。我也看到了this 的建议,但我不确定如何将它应用到我自己的代码中。当我单击更新时,它显示“UsersController#update 中的 NoMethodError”私有方法“update_without_password”调用 nil:NilClass
我的应用返回所有用户的参数,所以我不明白这里发生了什么。对 Rails 还是很陌生 - 如果您需要任何进一步的信息,请告诉我。提前谢谢!
用户控制器
class UsersController < ApplicationController
before_filter :authenticate_user!
def update
if user_params[:password].blank?
user_params.delete(:password)
user_params.delete(:password_confirmation)
end
successfully_updated = if needs_password?(@user, user_params)
@user.update(user_params)
else
@user.update_without_password(user_params)
end
respond_to do |format|
if successfully_updated
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml, :html)
end
private
def needs_password?(user, params)
params[:password].present?
end
def update_without_password(user_params)
user.update_without_password(user_params)
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :name, :role_id, :accepted)
end
编辑表格
<h3><%= @user == @current_user ? "Your Account Settings" : "Edit User" %></h3>
<%= form_for(@user, :html => { :method => :put }) do |f| %>
<p><%= f.label :first_name %></p>
<p><%= f.text_field :first_name %></p>
<p><%= f.label :last_name %></p>
<p><%= f.text_field :last_name %></p>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<% if can? :read, Role %>
<%= collection_select(:user, :role_id, Role.all, :id, :name, {prompt: true}) %>
<% end %>
<p><%= f.submit "Update" %></p>
<% end %>
我在这里缺少什么?请让我知道是否应该提供其他任何东西。谢谢!!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 authentication devise cancancan