Dan Sadaka
2011-Oct-05 22:24 UTC
Rails 3 routes with same name, different param name based on constraint
Hello there fellow RoR enthusiasts,
I have been wrestling with rails 3 routes for hours trying to accomplish
the following:
I want to use the *same url helper* (photo_url) in my views but have a
different route match made depending on the format of the parameter I
pass. Seems simple enough, right??
If the parameter looks like nn.nn.nnnnnn.whatever, I want the param
passed to "photos#show" to be called :site_photo_id. If the parameter
looks like an integer, I want the param passed to be :id.
I have tried the following:
match "/photos/:site_photo_id", :to => "photos#show", :as
=> "photo",
:constraints => {:site_photo_id => /\d{2}\.\d{2}\..*/}
match "/photos/:id", :to => "photos#show", :as =>
"photo",
:constraints => {:id => /\d+/}
If, in my view, I use a string like
"/photo/#{photo.id}"
it works.
But, again, I want to be able to use
photo_url(photo.id)
Can I do that?
TIA,
Dan
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Kleber Shimabuku
2011-Oct-06 07:56 UTC
Re: Rails 3 routes with same name, different param name based on constraint
Hi, What about to create a default route and just let the manage with the controller? Is it too wrong? On Oct 6, 7:24 am, Dan Sadaka <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello there fellow RoR enthusiasts, > > I have been wrestling with rails 3 routes for hours trying to accomplish > the following: > > I want to use the *same url helper* (photo_url) in my views but have a > different route match made depending on the format of the parameter I > pass. Seems simple enough, right?? > > If the parameter looks like nn.nn.nnnnnn.whatever, I want the param > passed to "photos#show" to be called :site_photo_id. If the parameter > looks like an integer, I want the param passed to be :id. > > I have tried the following: > > match "/photos/:site_photo_id", :to => "photos#show", :as => "photo", > :constraints => {:site_photo_id => /\d{2}\.\d{2}\..*/} > > match "/photos/:id", :to => "photos#show", :as => "photo", > :constraints => {:id => /\d+/} > > If, in my view, I use a string like > > "/photo/#{photo.id}" > > it works. > > But, again, I want to be able to use > > photo_url(photo.id) > > Can I do that? > > TIA, > Dan > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dan Sadaka
2011-Oct-06 13:36 UTC
Re: Rails 3 routes with same name, different param name based on constraint
Kleber Shimabuku wrote in post #1025300:> Hi, > > What about to create a default route and just let the manage with the > controller? > > Is it too wrong?I want the parameter name to correctly identify the source of the image so the controller knows where to get it. thx, D -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer
2011-Oct-06 16:41 UTC
Re: Rails 3 routes with same name, different param name based on constraint
Seems like it would be a lot easier to just have the logic in the
controller.
Right now you have the logic in the routes, then you''ll also need logic
in
the controller to check whether params[:id] or params[:site_photo_id]
exists.
If you have just one route and move the logic to the controller, you''ll
accomplish what you''re looking for, and avoid repeating logic in routes
and
controller.
*routes.rb*
match "/photos/:id", :to => "photos#show", :as =>
"photo", :constraints =>
{:id => /.*/}
*photos_controller.rb*
def show
if params[:id] =~ /\d{2}\.\d{2}\..*/
# it''s a site_photo_id
end
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/7WL0cu_v1jcJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.