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

67 views
Skip to first unread message

Ronald Fischer

unread,
May 23, 2014, 5:36:42 AM5/23/14
to rubyonra...@googlegroups.com
(Crossposting Note: This topic has been posted already at
https://railsforum.com/topic/1933-link-to-with-array-parameter-for-url-what-does-it-mean
, but did not get a reply so far)

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?

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
May 23, 2014, 8:58:10 AM5/23/14
to rubyonra...@googlegroups.com
What you are seeing is evidence that there is a nested route in play. Have a read of the Routing from the outside in Rails Guide, and note carefully what happens when you do this:

resources :articles do
resources :comments
end

What do the URL helpers look like to navigate to a single article's comments, or to an individual comment itself? Remember, there is no exposed /comments/123 URL possible once you do that scheme above.

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/778fe1221ef4104cc28d0909c40fd1c1%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Ronald Fischer

unread,
May 25, 2014, 2:13:49 AM5/25/14
to rubyonra...@googlegroups.com
Thanks for the explanation! I noticed your response only now, and it
would have at least partially answered my other question I had posted in
this forum too ;-)

I think I get the idea with nested routing now. What I still don't
understand is a *syntactic* issue: How to map this feature to the API
description. Looking at, for example,

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

I can not find a link_to with a signature which would allow for an array
as the second parameter. Is this an omission from the API description
(which I maybe should report to the maintainers), or did I just look at
the wrong place?

Walter Lee Davis

unread,
May 25, 2014, 8:46:32 AM5/25/14
to rubyonra...@googlegroups.com
Think of the array of elements as the first variable passed to the method. You can have three or more elements in that array, if that's how deep the nesting goes. But the array itself is the first variable, and in that limited way of looking at this, means that the documentation is correct to a point. I agree, it would be good if there was a signature showing this in the API docs. Good thing that Rails is open source -- you can get a commit out of this if you write it up and submit a pull request on GitHub!

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7331344edac494094c8eeaf648c68c48%40ruby-forum.com.

Ronald Fischer

unread,
May 25, 2014, 9:17:51 AM5/25/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1147032:
> Think of the array of elements as the first variable passed to the
> method. You can have three or more elements in that array, if that's how
> deep the nesting goes.

Well, this part I understand; it's just the usual parameter passing, and
of course you can pass any parameter (and, actually, it is the second
parameter, not the first, but this is a minor issue). But ...
> But the array itself is the first variable, and
> in that limited way of looking at this, means that the documentation is
> correct to a point.

... I don't quite agree. Looking at the description of the possible
signatures, the documentation says:

=====

link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
# posts_path

link_to(body, url_options = {}, html_options = {})
# url_options, except :method, is passed to url_for

link_to(options = {}, html_options = {}) do
# name
end

link_to(url, html_options = {}) do
# name
end

=====

In our case, the first of the signatures applies for our nested routes
example (because all the other ones have a hash at the second
parameter), and the *description* for it says: "url is a String". I
thing this should be instead "url is a String or an object or an Array
of objects, and the objects should be a subtype of ActiveRecord::Base".
Do you think this would catch it? If not, it means that I'm still
lacking some point of understanding here.

> I agree, it would be good if there was a signature
> showing this in the API docs. Good thing that Rails is open source --
> you can get a commit out of this if you write it up and submit a pull
> request on GitHub!

Maybe I would better submit a correction to the docs to the email
address mentioned at the bottom of the apidock.com page. I'm still
pretty new in the development with Rails, as you certainly have noticed,
and at this stage, "fixing" something - even if it is only in the docs -
could do more harm than good.

Ronald
Reply all
Reply to author
Forward
0 new messages