Its easy to determine URL in Rails

Rails has made so much easy to generate URL for RESTful resources. You don’t have to write the long route helper method; there exits a short hand method to be used; like

url_for [:edit, @parent_object, @resource_object]


When using book_article_path, you can just pass in instances of Book and Article instead of the numeric IDs:

<%= link_to 'Article details', book_article_path(@book, @article) %>

Moreover, you can use url_for with a set of objects, and Rails will automatically determine which route you want to generate:

<%= link_to 'Ad details', url_for([@book, @article]) %>

If you want any specific :action then try

<%= link_to 'Ad details', url_for([:edit, @book, @article]) %>

Some More

If you wanted to link to just a book:

<%= link_to 'Book details', @book %>

If you want to link to nested resource then

<%= link_to 'Article details', [@book, @article] %>

Note: the above technique will only work with link_to helper method.



Some useful links

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s