gpt4 book ai didi

ruby-on-rails - UsersController 中的 NoMethodError#update

转载 作者:太空宇宙 更新时间:2023-11-03 16:36:55 26 4
gpt4 key购买 nike

错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]


app/controllers/users_controller.rb:45:in `update'

Controller users_controller

def update
@user.groups = Group.find(@params[:group_ids]) if @params[:group_ids]
if @user.update_attributes(params[:user])
flash[:success] = "User updated."
#Redirect back to current users, i've change this as after the admin edit a user it would redirect them
#Too that users account which conflicts.
#if current_user.admin? # If the current user is an admin move them back to the user list page.
#redirect_to users_path
#else # if its a normal user just redirect them back to there dashboard
redirect_to @user
#end
else
@title = "Edit user"
render 'edit'
end
end

View (edit.html.erb):

<% for group in @groups %>
<%= check_box_tag "user[group_ids][]", group.id %>
<%= group.description %>
<% end %>

最佳答案

@user.groups = Group.find(@params[:group_ids]) if @params[:group_ids]

@user.groups = Group.find(params[:group_ids]) if params[:group_ids]

关于ruby-on-rails - UsersController 中的 NoMethodError#update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com