开发者

How do I edit one model from another's controller?

I have 2 models, user and group. user has_one group and group has_many users.

I have a admin role, and a group admin role in my can can ability class that I want to be able to remove a user from the group.

I开发者_StackOverflown my show members view in the controller, I have an if statement that adds a remove button next to a user in the list if the viewer is an admin or group admin.

#views/groups/show_members.erb
<% form_for @user, :remote => true, :url => { :controller => 'groups', :action => 'remove_user' } do |f| %>
  <%= f.hidden_field :user_id, :value => @user.id %>
  <%= f.hidden_field :group_id, :value => "" %>
  <%= image_submit_tag("remove-icon.png") %>
<% end %>

#groups_controller.rb
def remove_user
  @user = User.find(params[:user_id])
  @user.group_id = nil
  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to(@group, :notice => 'User was successfully Removed.') }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @group.errors, :status => :unprocessable_entity }
    end
  end
end

The error I get is:

Couldn't find User without an ID
Rails.root: /home/leon/sites/VIS

Application Trace | Framework Trace | Full Trace
app/controllers/groups_controller.rb:118:in `remove_user'
Request

Parameters:

{"x"=>"3",
 "y"=>"9",
 "authenticity_token"=>"0BDDZDMR9ILzfnQkzoKwZD4u7tjC+oPHb9Ijpln+P9w=",
 "_method"=>"put",
 "utf8"=>"✓",
 "user"=>{"group_id"=>"",
 "user_id"=>"6"}}


Since you're using form_for, all of values submitted through your f. methods are going to end up in params[:user], not in params. You can either keep the view as-is, and retrieve the value from params[:user][:user_id] or you can use the hidden_field_tag helper instead of f.hidden_field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜