简体   繁体   中英

how do I use link_to link to different controller and model?

I currently have the following for-loop. This is in the ' view/vendor/show.html '.

I set up a ' vendor :has_many :reviews ' and this is the for-loop:

<% for review in @vendor.reviews %>

    <%= review.user_id %>
    <%= review.summary %><br />

    <%= link_to 'More', @review%>

    <hr class="left span-5" />

<% end %>

For the link_to I would like it to link to the URL: reviews/:review_id

thanks!

Your code right now wouldn't work,

link_to 'More', @review

you want just review (without the @) @review is not defined anywhere in your code.

Assuming you have your routes set up properly,

link_to 'More', review

should suffice. Of course Brian's code does the same thing (again, without the @ though). Rails can automatically figure out the proper path if you just use the object directly (using polymorphic routes) or with brian's:

link_to 'More', review_path(review)

Is the same as review_path(review.id) Rails automatically takes the id of the object being referenced

<%= link_to "More", review_path(@review) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM