Jump to content

The ultimate community for Ruby on Rails developers.


link_to with array parameter for URL : What does it mean?

link_to url

  • Please log in to reply
2 replies to this topic

#1 rovf



  • Members
  • 4 posts

Posted 21 May 2014 - 09:06 AM

I found in http://guides.rubyon...-a-partial-form the following example:


link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }


I was surprised to see an array as second parameter for link_to and would like to know the meaning of this construct. The website gives just the somewhat thin explanation Clicking this new "Destroy Comment" link will fire off a DELETE /articles/:article_id/comments/:id to our CommentsController, which explains the effect of this statement, but does not really explain the general mechanism behind this concept. I've looked up the API documentation at http://api.rubyonrails.org/, which gives for the second parameter of link_to the following choices (for the case that the first parameter is a string):


  • A string containing the URL to link to, or
  • A hash containing various options for the URL

An array as parameter is not mentioned. It seems that the API documentation is not complete here. Or did I look at the wrong place?


Please could someone explain this syntax to me, and also point out, where I can find a more detailed description of the API?


I'm using Rails 4.1.1.



#2 rovf



  • Members
  • 4 posts

Posted 23 May 2014 - 09:37 AM

(Note: I have crossposted this topic here: https://www.ruby-for.../topic/4859346)

#3 Clint Bishop

Clint Bishop


  • Members
  • 1 posts

Posted 21 July 2014 - 02:47 AM   Best Answer



The short answer to the question is: an array containing an Article of id 1 and a comment of id 2 will translate into this URL: /articles/1/comments/2.


I wrote a little more on the subject here: http://norobotdiving..._to-with-array/. Check it out!

  • Jamie and rovf like this

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users