Forum: Ruby on Rails nested rest URL => passing param from select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
01ff060e4998723d020cd0ad6fa8c7ad?d=identicon&s=25 Maarten Porters (temesta)
on 2007-06-22 13:23
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?
624c731a73c56e2d8da1c5a9b3a7fa14?d=identicon&s=25 Chris Kampmeier (kampers)
on 2007-06-24 03:33
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
This topic is locked and can not be replied to.