Luigi
Luigi

Reputation: 5603

Rails routing - Passing unique id into url

I have the following in a view:

<dl class="dl-horizontal">
<% @people.each do |person| %>
<dt> <%= person[:name] %> </dt>
<dd> <%= button_to('Check', person_check_path, :class => 'btn btn-medium btn-primary', :method => :get)  %>
<% end %>

The url represented by this view is: www.website.com/{town_id}/person/

person_check_path looks like this:

www.website.com/{town_id}/person/{person.id}/check

My question is how can I pass person.id into the person_check_path dynamically? Right now the above button links to the same url everytime, but I need it to change based on which person the button is clicked for.

Upvotes: 0

Views: 859

Answers (1)

damoiser
damoiser

Reputation: 6238

First you should add the new get request in the routes, something like that:

get "/:town_id/person/:person_id/check" => "town#check", :as => :check_town_with_person

Then you can call the route from anywhere, for example:

link_to "My Link", check_town_with_person_path(:town_id => @town.id, :person_id => @person.id)

Remember that these fields (setted in the routes) are mandatory. You can even add some not mandatory params in this manner:

 link_to "My Link", check_town_with_person_path(:town_id => @town.id, :person_id => @person.id, :another_var => "my_var")
=> /3/person/1/check?another_var=my_var

Upvotes: 3

Related Questions