我有 2 个模型,用户和组。用户 has_one 组和组 has_many 用户。
我有一个管理员角色,并且我的 can can 能力类中有一个组管理员角色,我希望能够从组中删除用户。
在控制器的显示成员视图中,如果查看者是管理员或组管理员,我有一个 if 语句,它在列表中的用户旁边添加一个删除按钮。
#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
我得到的错误是:
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"}}