Hello. I have a strange issue I can''t figure out: In routes.rb I create this route: map.connect(''/api/v2/store/individual_offer/:offer_id'', { :controller => ''/api/v2/store'', :action => :individual_offer , }) Then, in the view I generate a url in this way: url_for(:controller => ''/api/v2/store'', :action => :individual_offer, :offer_id => offer[:id]) What the generated url ends up looking like is this, though: lukka.local:7000/api/v2/store/individual_offer?offer_id=43, which results in url getting associated with a different route from the one I mentioned above. This is the very first route pointing to controller ''/api/v2/store'', so it should be tried first. Did anyone encounter such issue before? I''m using Rails 2.3.11. Thanks! Luka -- 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.
On Thu, Apr 7, 2011 at 4:50 PM, Luka Stolyarov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello. I have a strange issue I can''t figure out: > > In routes.rb I create this route: > > > map.connect(''/api/v2/store/individual_offer/:offer_id'', { > :controller => ''/api/v2/store'', > :action => :individual_offer , > }) > > Then, in the view I generate a url in this way: > > url_for(:controller => ''/api/v2/store'', :action => :individual_offer, > :offer_id => offer[:id]) > > What the generated url ends up looking like is this, though: > > lukka.local:7000/api/v2/store/individual_offer?offer_id=43, which > results in url getting associated with a different route from the one I > mentioned above. > > This is the very first route pointing to controller ''/api/v2/store'', so > it should be tried first. Did anyone encounter such issue before? I''m > using Rails 2.3.11. > > Thanks! > Luka >The views url_for is doing exactly what it thinks you want. By passing :offer_id in with the hash it is treating it as a parameter that you want tacked onto the url string. If want you want is this: lukka.local:7000/api/v2/store/individual_offer/43 then you would need to change your view to pass in object of individual_offer into url_for. url_for(@individual_offer) B. -- 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.
Works like a charm. Thanks a lot! -- 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.
On 7 April 2011 23:39, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thu, Apr 7, 2011 at 4:50 PM, Luka Stolyarov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Hello. I have a strange issue I can''t figure out: >> >> In routes.rb I create this route: >> >> >> map.connect(''/api/v2/store/individual_offer/:offer_id'', { >> :controller => ''/api/v2/store'', >> :action => :individual_offer , >> }) >> >> Then, in the view I generate a url in this way: >> >> url_for(:controller => ''/api/v2/store'', :action => :individual_offer, >> :offer_id => offer[:id]) >> >> What the generated url ends up looking like is this, though: >> >> lukka.local:7000/api/v2/store/individual_offer?offer_id=43, which >> results in url getting associated with a different route from the one I >> mentioned above. >> >> This is the very first route pointing to controller ''/api/v2/store'', so >> it should be tried first. Did anyone encounter such issue before? I''m >> using Rails 2.3.11. >> >> Thanks! >> Luka > > The views url_for is doing exactly what it thinks you want. By passing > :offer_id in with the hash it is treating it as a parameter that you want > tacked onto the url string.Well, not really. You should be able to pass parameters in like that and get them matched up to named segments in route definitions. A lot of apps (pre the Rails REST revolution) do this. In this case, it works if you change your route definition and your url_for call to not have the initial slash in the controller name: map.connect(''/api/v2/store/individual_offer/:offer_id'', { :controller => ''api/v2/store'', :action => :individual_offer }) <%= url_for(:controller => ''api/v2/store'', :action => :individual_offer, :offer_id => ''43'') %> This produces /api/v2/store/individual_offer/43 as you''d hope. I think there is some normalisation that goes on with regard to removing initial slashes from nested controller names, and I suspect that the original code was simply not matching the special route (and hence using the default route instead) because of this difference between initial slash in the url_for call and no initial slash in the normalised route definition. That''s just a hunch, though.> If want you want is this: > > lukka.local:7000/api/v2/store/individual_offer/43 > > then you would need to change your view to pass in object of > individual_offer into url_for. > > url_for(@individual_offer)That''s also a solution for this case, but it''ll fall down if there are any more complicated custom routes with named segments that you want to specify. Chris -- 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.
That worked, as well. Thank you so much! -- 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.