2011-07-05 38 views
2

錯誤:NoMethodError在UsersController#更新

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' 

控制器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 

的視圖(edit.html.erb):

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

回答

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

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

感謝它的工作,這是一個愚蠢的錯誤。 – ahmet

+0

旁觀者看到了大部分的遊戲;) – Bohdan

相關問題