views:

43

answers:

1

This is a very basic question with probably an easy answer. Let's say I have a model called Product. When I add

map.resources :products

to my routes.rb I have access to some default paths new_product_path, edit_product_path and so on. As I understand this should be used when linking to a resource e.g. using the link_to helper method:

link_to "Edit Product", edit_product_path(@product)

My question is: What do I do when I have other controller methods like for example

def do_something
  ...
end

What's the "best" way to link to this controller method?

link_to "Do Something", {:controller => 'products', :action => 'do_something', :id => @product.id}

for sure would work. But is that what I should use?

I hope I made my point clear! Please comment if not. I'll try to explain it better then.

+1  A: 

You might want to read up on the Rails Routing, specifically, adding a new Restful route.

If you use this method, I believe it is fairly simple:

# routes.rb
map.resources :products, :member => { :do_something => :get }

# your view.rb file
link_to "Do Something", do_something_product_path(object_id)

Also make sure you read the section regarding "collection routes" if you want the URL to operate on all of the products (like a more complex index page).

Topher Fangio
thanks for the fast answer
StefanS
@Stefan@ - Glad to help :-)
Topher Fangio