How to Pass a Selected Option Value of select_tag from One Partial to Another

Hi,

I'm running Rails 2.3.5 & Ruby 1.8.7 on Windows XP.

I have two partials: 1. File app\views\warehousing\_index.html.erb has       .....       <%= select_tag(:filter_by_category, "<option>-Select a category- </option>" +

options_from_collection_for_select(Categories.find(:all, :order => 'name'), :name, :name)) %>       .....       <%= render :partial => 'warehousing/entry', :locals => {:filter_by_category => "Hello World"} %>       ..... 2. File app\views\warehousing\_entry.html.erb has       .....       <div>Selected category is <%= filter_by_category %></div>       .....

On the displayed page, it shows:

      Selected category is Hello World

That means the string "Hello World" in variable filter_by_category is indeed passed from one partial to the other. For my project, I want to pass the selected option value from the select_tag to partial _entry.html.erb, i.e. in file _entry.html.erb, for line     <%= render :partial => 'warehousing/entry', :locals => {:filter_by_category => ???} %> what should I put in for "???".

Thanks for any help that you could offer. Hoca

Hoca wrote in post #955056:

Hi,

I'm running Rails 2.3.5 & Ruby 1.8.7 on Windows XP.

Why on earth are you doing Rails development on Windows? At least get a *nix VM.

I have two partials: 1. File app\views\warehousing\_index.html.erb has       .....       <%= select_tag(:filter_by_category, "<option>-Select a category- </option>" +

options_from_collection_for_select(Categories.find(:all, :order => 'name'), :name, :name)) %>       .....       <%= render :partial => 'warehousing/entry', :locals => {:filter_by_category => "Hello World"} %>       ..... 2. File app\views\warehousing\_entry.html.erb has       .....       <div>Selected category is <%= filter_by_category %></div>       .....

On the displayed page, it shows:

      Selected category is Hello World

That means the string "Hello World" in variable filter_by_category is indeed passed from one partial to the other. For my project, I want to pass the selected option value from the select_tag to partial _entry.html.erb, i.e. in file _entry.html.erb, for line     <%= render :partial => 'warehousing/entry', :locals => {:filter_by_category => ???} %> what should I put in for "???".

It's not possible, because at the time the partial is rendered, the user has not yet had a chance to select anything -- that is, by the time the user sees the page in the browser, _entry has already been rendered and it is now too late to change it.

Therefore, you'd need to use Ajax to send a request to the server which would re-render _entry when a new category is selected.

Thanks for any help that you could offer. Hoca

Best,

Hi Marnen,

Thanks so much for your suggestion. I'll try another approach then.

Hoca.