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

ruby-on-rails

Solution

Nesting resources route from an URL that contains the ID of both resources, in this case something like: `/projects/1/tickets/10`. To generate this URL, we need to know the id of both the project and the ticket, so both of these objects need to be passed in.

An edit URL goes further, and adds an action keyword - `/projects/1/tickets/10/edit`, so again we need to pass in this action.

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

For more information, I'd recommend reading through Rails Routing from the Outside In

Problem

I am reading Rails 3 In Action. The book builds a class of Projects which `has_many :tickets` and a class of Tickets which `belongs_to :project`. The routes.rb file looks like this: ``` resources :projects do resources :tickets end ``` Now the form for creating a ticket takes in an array like so: ``` <%= form_for [@project, @ticket] do |f| %> ``` And on the ticket show.html.erb page there are links which 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 the array of the two objects needs to be passed into form_for() and into link_to(). Also, why does "Edit Ticket" require and :edit symbol while "Delete Ticket" does not require a :destroy symbol. thanks, mike

Original source