【发布时间】:2012-05-02 18:30:47
【问题描述】:
我正在使用 Devise 和 CanCan 来管理用户和用户权限。 我想要的是:普通用户应该能够更新自己的帖子。 最重要的模型称为 Activity。
在我对普通会员的能力模型中:
elsif user.role? :Member
can :read, :all
can :create, :all
can :manage, Activity, :user_id=>user.id
(感谢 Yuriy Goldshtrakh 提供第三行的语法)
在活动的索引视图中,我有:
<% if can? :update, activity %>
<br />
<%= link_to 'Update', edit_activity_path(activity) %>
<% end %>
<% if can? :delete, activity %>
<%= link_to 'Delete', activity, :confirm => 'Really?', :method => :delete %>
<% end %>
这有效:它只显示 更新和删除链接,如果 Activity 是由当前成员创建的。
但是,如果成员更新活动,则不会保存更改,并且不会将成员发送回活动 - 因为他/她应该在成功更新后。
这是活动控制器中的更新操作:
# PUT /activities/1.xml
定义更新
authorize! :update, @activity
@activity = Activity.find(params[:id])
respond_to do |format|
if @activity.update_attributes(params[:activity])
format.html { redirect_to(@activity, :notice => 'Activity was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @activity.errors, :status => :unprocessable_entity }
end
end
结束
问题是:为什么活动没有正确更新? 我很感激所有的想法!
【问题讨论】:
标签: ruby-on-rails devise cancan