Reputation: 3870
I have a model named product. What i wanted to be able to do was write "product.link" in my view to generate a "link_to product.title, product". I know I can't define the "link" method in the Product.rb file (because link_to does not work there), and i don't want to write "link_to product.title, product" every time i need to create a link to the product.
I was wondering if anyone could suggest the ideal way to go about having a minimal simple way of generating links to my products.
I was also wondering if there was a way in rails to set the default label field to show when i write "link_to product"
instead of what it shows now: "#<Product:0x105093f20>"
Upvotes: 0
Views: 1049
Reputation: 26528
Add a helper method which does the appropriate thing:
# products_helper.rb
def product_link(product)
# Change these to taste
link_to product.name, product_path(product)
end
Now in your view you can call the following in your view:
product_link product
As for your question about #
appearing in the links, this is the link_to
helper calling to_s on the object for the html portion of the link. Use a helper as above to define the default text.
Upvotes: 2