# Nested rest URL =\> passing param from select

**URL:** <https://www.ruby-forum.com/t/nested-rest-url-passing-param-from-select/107515>\
**Category:** Rails\
**Created:** [June 22, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/nested-rest-url-passing-param-from-select/107515 "2007-06-22T13:23:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![temesta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9bcb4/32.png) [@temesta](https://www.ruby-forum.com/u/temesta)\
**Post date:** [June 22, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/nested-rest-url-passing-param-from-select/107515/1 "2007-06-22T13:23:43Z")

</div>

Hi,

I’m trying to create a select box with an onchange event. When onchange  
is fired, I want to redirect to a nested rest url. The problem is that I  
don’t know how to pass the selected value to the url:

Any suggestions?

---

<div class="post-metadata">

**Author:** ![temesta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9bcb4/32.png) [@temesta](https://www.ruby-forum.com/u/temesta)\
**Post date:** [June 24, 2007, 3:33am UTC](https://www.ruby-forum.com/t/nested-rest-url-passing-param-from-select/107515/2 "2007-06-24T03:33:36Z")

</div>

Maarten P. wrote:

> Hi,
> 
> I’m trying to create a select box with an onchange event. When onchange  
> is fired, I want to redirect to a nested rest url. The problem is that I  
> don’t know how to pass the selected value to the url:
> 
> Any suggestions?

I did this recently by creating a RESTful RedirectionsController. I  
wanted a select box to redirect me to a #new action.

# in routes.rb

map.resources :redirections

map.resources :activities do |map|  
map.resources :exercises, :name\_prefix =\> ‘’  
end

# in the view with the select box

\<% form\_tag(redirections\_path, :method =\> :post) do %\>  
\<%= select “exercise”, “activity\_id”, Activity.find(:all).collect  
{|act| [act.name, act.id] } %\>  
\<%= hidden\_field\_tag ‘redirection’, ‘new\_exercise’ %\>  
\<%= submit\_tag “Add a new exercise for this activity” %\>  
\<% end %\>

# in redirections\_controller.rb

class RedirectionsController \< ApplicationController

# POST /redirections

# POST /redirections.xml

def create  
case params[:redirection]  
when ‘new\_exercise’  
redirect\_to new\_exercise\_path(params[:exercise][:activity\_id])  
else  
raise ArgumentError  
end  
end

end

The idea is that you POST a string in params[:redirection], and an ID  
that tells is where to redirect to. This could be DRYer, but it’s a big  
improvement from the alternative (hardcoding your routes in your views  
or somesuch).

Hope this helps.

Chris K.  
[http://kampers.net](http://kampers.net)
