# Rails Routes using complex parameter names

**URL:** <https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040>\
**Category:** Rails\
**Created:** [October 10, 2009, 9:32am UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040 "2009-10-10T09:32:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wbr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b3f665/32.png) [@wbr](https://www.ruby-forum.com/u/wbr)\
**Post date:** [October 10, 2009, 9:32am UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040/1 "2009-10-10T09:32:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wbr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b3f665/32.png) [@wbr](https://www.ruby-forum.com/u/wbr)\
**Post date:** [October 10, 2009, 10:24am UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040/2 "2009-10-10T10:24:40Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wbr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b3f665/32.png) [@wbr](https://www.ruby-forum.com/u/wbr)\
**Post date:** [October 10, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040/3 "2009-10-10T23:35:01Z")

</div>

On Oct 9, 5:24 pm, wbr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![wbr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b3f665/32.png) [@wbr](https://www.ruby-forum.com/u/wbr)\
**Post date:** [October 11, 2009, 12:06am UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040/4 "2009-10-11T00:06:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wbr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b3f665/32.png) [@wbr](https://www.ruby-forum.com/u/wbr)\
**Post date:** [October 11, 2009, 12:12am UTC](https://www.ruby-forum.com/t/rails-routes-using-complex-parameter-names/177040/5 "2009-10-11T00:12:31Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
