Route not getting recognized

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

On Thu, Apr 7, 2011 at 4:50 PM, Luka S. [email protected]
wrote:

Then, in the view I generate a url in this way:
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.

Works like a charm. Thanks a lot!

On 7 April 2011 23:39, Bryan C. [email protected] wrote:

})
mentioned above.
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

That worked, as well. Thank you so much!