ChrisMJ
ChrisMJ

Reputation: 1620

If Statement within Views in Rails 3

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 following 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 %>

Upvotes: 2

Views: 6285

Answers (1)

Marcel Jackwerth
Marcel Jackwerth

Reputation: 54762

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 %>

Upvotes: 4

Related Questions