Maarten Porters 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:
>
> <select id="category" onchange=" <%=
remote_function(:url =>
> category_articles_path(selected value?) ) %> ">
>
>
> 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 Kampmeier
http://kampers.net
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---