Why is .id needed for the parameter in the edit_recipe_path?


#1



https://www.codecademy.com/en/courses/rails-auth/lessons/authorization/exercises/authorization-admin-role-ii

6.) Admin Role II
Just a general question, why is .id needed for the parameter in the edit_recipe_path but not the delete path recipe_path ?

views/recipes/show.html.erb

...

<% if current_user && current_user.editor? %>
<p class="recipe-edit">
  <%= link_to "Edit Recipe", edit_recipe_path(@recipe.id) %>
</p>
<% end %>


<% if current_user && current_user.admin? %>
<p class="recipe-delete">
  <%= link_to "Delete", recipe_path(@recipe), method: 'delete' %>
</p>
<% end %>

</div>
</div>

#2

Hey @tutors2go,

Great question! The id isn't needed for either, however both will work with (or without) it. I can't find where anymore, perhaps it's changed and I didn't notice, but it used to be best practice to omit the .id at the end and pass in the entire object, in this case @recipe.

So, it'll work with or without just the ID, but it is or used to be considered best practice to pass in the object and not just the ID.

I hope this helps! You can read more about link_to here


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.