开发者

If Statement within Views in Rails 3

开发者 https://www.devze.com 2023-03-11 14:48 出处:网络
Ok so i have been starting to get used to rails 3 over the past few days and have got a project in the works to test things out on. Is it possible to do the fo开发者_运维问答llowing or what would you

Ok so i have been starting to get used to rails 3 over the past few days and have got a project in the works to test things out on. Is it possible to do the fo开发者_运维问答llowing or what would you suggest is the best way to only allow post authors to edit their posts.

<% if post.author_id == current_user.id %>
   <td><%= link_to 'Edit', edit_post_path(post) %></td>
   <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>


Recommendation: Don't compare ids - compare objects.

<% if post.author == current_user %>

Optional: Consider using a plugin (only if necessary) like cancan to make it even more descriptive.

<% if can? :edit, post %>
0

精彩评论

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