Rails Routes using complex parameter names

I have a search form that generates ugly but functional urls.The
controller takes nested parameters from the url like program
[program_id], or program[leader][leader_id].

So I’m trying to create a named route to make certain pre-defined
searches easier for customers to embed in their own pages.

So instead of

 "https://ourdomain.com/search?program[leader_id]=12345"

Id like to have

 "https://ourdomain.com/leader/12345"

The problem is that a named Rails route won’t accept a complex symbol
in the path definition:

 map.leader     'leader/:program[leader_id]'

fails due to the brackets in parameter name symbol. Even properly
defining the symbol as

 map.leader     "leader/:'program[leader_id]'"

fails. Rails just won’t recognize anything other than a downcase
single word.

… and I’m not in a position to change how the controller works or
redefine the expected incoming parameters.

Thoughts or suggestions or workarounds?

Thanks,
Bill

wbr wrote:

I have a search form that generates ugly but functional urls.The
controller takes nested parameters from the url like program
[program_id], or program[leader][leader_id].

So I’m trying to create a named route to make certain pre-defined
searches easier for customers to embed in their own pages.

So instead of

 "https://ourdomain.com/search?program[leader_id]=12345"

Id like to have

 "https://ourdomain.com/leader/12345"

The problem is that a named Rails route won’t accept a complex symbol
in the path definition:

 map.leader     'leader/:program[leader_id]'

fails due to the brackets in parameter name symbol. Even properly
defining the symbol as

 map.leader     "leader/:'program[leader_id]'"

fails. Rails just won’t recognize anything other than a downcase
single word.

So in the controller, just do params[:program][:leader_id] =
params[:leader_id] . Or introduce a LeadersController.

… and I’m not in a position to change how the controller works or
redefine the expected incoming parameters.

Why aren’t you? If you’re going to develop the app, you need to be able
to make the necessary changes.

Thoughts or suggestions or workarounds?

Thanks,
Bill

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Oct 9, 5:24 pm, wbr [email protected] wrote:

… and I’m not in a position to change how the controller works or
redefine the expected incoming parameters.

Thoughts or suggestions or workarounds?

Get a new gig. Seriously, any place that ties your hands like this
isn’t worth putting up with.

If you’re really set on working with the “can’t modify the controller
but can change routes.rb” system, a truly sneaky dev could re-open the
controller at the bottom of routes.rb (it’s loaded with ‘load’, so you
can whack any code in there) and do something like this:

class SearchController
before_filter :munge_params

def munge_params
params[:program][:leader_id] = params[:leader_id] unless params
[:leader_id].blank?
end

end

But don’t do that. Either you’re a developer or not - fencing off bits
of the app doesn’t make any sense.

–Matt J.

While I appreciate the feedback, it’s not the direction I’m looking
for. This rails app is huge, with views and controllers designed
around a specific and complex structure. Adding controllers or
restructuring the controller/view architecture every time someone
wants to create a pretty route for a specific search, is not really
the Rails way is it? To me, that would be sloppy programming.

To clarify my original question:

I want to create a route that parses path segments to parameters with
complex keys:

map.programs 'programs/:program[program_type]/:program

[:leader_id]’, :controller=>…, :action=>…

I’m looking for a way to tweak the route definition or to remap rout-
definition-compatible parameters, so that by the time they reach the
controller they have the correct key names.

wbr wrote:

While I appreciate the feedback, it’s not the direction I’m looking
for. This rails app is huge, with views and controllers designed
around a specific and complex structure. Adding controllers or
restructuring the controller/view architecture every time someone
wants to create a pretty route for a specific search, is not really
the Rails way is it?

No. But you’ve at least got to make sure the controller expects
parameters with reasonable names.

To me, that would be sloppy programming.

And to me too. But that’s not the issue here.

To clarify my original question:

I want to create a route that parses path segments to parameters with
complex keys:

map.programs 'programs/:program[program_type]/:program

[:leader_id]’, :controller=>…, :action=>…

I’m looking for a way to tweak the route definition or to remap rout-
definition-compatible parameters, so that by the time they reach the
controller they have the correct key names.

Just introduce a routine in the controller that transforms the parameter
names in some generic way. Problem solved.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs