Passing to an array in form_for () and link_to ()

I am reading Rails 3 in action. The book creates a class of projects that has_many :tickets and a class of tickets that belongs_to :project . The route.rb file looks like this:

  resources :projects do resources :tickets end 

Now the form for creating a ticket accepts such an array:

  <%= form_for [@project, @ticket] do |f| %> 

And on the ticket page show.html.erb there are links that look like this:

  <%= link_to "Edit Ticket", [:edit, @project, @ticket] %> <%= link_to "Delete Ticket", [@project, @ticket], :method => :delete, :confirm => "Are you sure you want to delete this ticket?" %> 

Now I'm confused why an array of two objects needs to be passed to form_for () and link_to (). In addition, why do you need to "Edit ticket" and: edit the symbol, and "Delete ticket" is not required: destroy the symbol.

thank you microphone

+4
source share
1 answer

Nested resources are routed from a URL that contains the identifier of both resources, in this case something like: /projects/1/tickets/10 . To generate this URL, we need to know the identifier of both the project and the ticket, so both of these objects must be transferred.

The edit URL goes further and adds the action keyword - /projects/1/tickets/10/edit , so we need to go through this action.

RESTful destroys the route in Rails, however it uses the HTTP DELETE method instead of the action keyword, so the url to destroy your ticket is really /projects/1/tickets/10 ; just with a DELETE request instead of a GET.

For more information, I would recommend reading Rails Routing from Outside In

+6
source

Source: https://habr.com/ru/post/1413885/


All Articles