Robert
Robert

Reputation: 311

What value can a Rails route helper have as a parameter?

I'm working on a Rails tutorial and I don't understand the value a route helper can have as parameter.

Why is it correct for the "article_path" to have "article" as its parameter?

<ul id=“articles”>
    <% @articles.each do |article| %>
        <li>
            <%= link_to article.title, article_path(article) %>
        </li>
    <% end %>
</ul>

Upvotes: 0

Views: 52

Answers (2)

Mayur Shah
Mayur Shah

Reputation: 3459

article_path(article)

Is valid Because you are passing the whole object as a parameter when parameters come to the controller, controller get the id from parameter and give a particular record.

I suggest you follow the standard way using article_path(article.id) because if you have number of fields in article model (table) then all filed are pass in the parameter.

Upvotes: 2

puneet18
puneet18

Reputation: 4427

In below route:

article_path(article)

you can pass article as whole object or article.id both are valid.

Upvotes: 1

Related Questions