Nested rest URL => passing param from select

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?

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