Question

I'm stuck on a routing problem within a Rails 3.0.x application.

What I'm trying to achieve is a URL like /registration/renew/1 . The idea is that this would renew registration for a member with id = 1.

So to that end I setup the following routes

routes.rb

match "registration/renew" => "registration#renew"

The user arrives at the registration page via a navigation link such as

<%= link_to "Full Member", registration_renew_path(@member)  %>

The problem is that the generated link comes out like: /registration/renew.1 which indicates that the :format extension is being created and appended. Which then I tried to make optional via inclusion of a responder argument :format as per the following matching rule

match "registration/renew(/:id(.:format))" => "registration#new"

but this fails with

No route matches {:controller=>"registration", :action=>"renew", :format=>#<Member id: 1,.....

So at this point I rechecked the Rails Guides etc but still couldn't get to generate the URL I was after.

Only when I had the two rules:

match "registration/renew" => "registration#renew"
match "registration(/:action(/:id(.:format)))" => "registration#renew"

in the routes file would the URL /registration/renew/1 get me to the page. Although I didn't feel that this was the correct, tidy solution.

The final question(s)

  1. What should be the link_to method
  2. What is the correct routes.rb entry

Thanks in Advance

Was it helpful?

Solution

You just need pass args explicitly and define the name of this route

match "registration(/:action(/:id(.:format)))" => "registration#renew", :as => registration_renew

With only id

<%= link_to "Full Member", registration_renew_path(:id => @member.id)  %>

With id and format

<%= link_to "Full Member", registration_renew_path(:id => @member.id, :format => :xml)  %>

Without id

<%= link_to "Full Member", registration_renew_path  %>

You don't need the route without params in your example.

OTHER TIPS

You can mention the controller and action in link_to method

<%=link_to "Full Member", :controller => "registration", :action => "renew", :id => @member.id %> 

Please refer the following tutorial for more options.

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top