我已经完成了从设计更改密码的单独链接,但我收到错误:
用户控件中的NoMethodError#更新未定义的方法`update'for 零:NilClass
有人可以帮助我!
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
if @user.update(user_params)
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
end
用户
中的edit.html.erb
文件
<%= form_for(@user) do |f| %>
<div class="row">
<label class="col-xs-4" for="password">New Password</label>
<div class="col-xs-8">
<%= f.password_field :password, autocomplete: "off", :placeholder => "New Password (leave blank if you don't want to change it)", :class => 'form-control' %>
</div>
</div></br></br>
<div class="row">
<label class="col-xs-4" for="password">Confirm Password</label>
<div class="col-xs-8">
<%= f.password_field :password_confirmation, autocomplete: "off", :placeholder => "Enter password to confirm changes", :class => 'form-control' %>
</div>
</div></br></br>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %>
</div></br></br>
<div class="actions">
<%= f.submit "Save", :class => "btn btn-primary" %>
</div>
<% end %>
</div></div>
我的错误是:
用户控件中的NoMethodError#更新未定义的方法`update'for 零:NilClass
答案 0 :(得分:1)
您需要在更新操作中设置用户变量
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit
end
end