Hi,
I have a dropdown menu which shows the categories available. When a user
selects one I want the browser to redirect to the selected category.
I now have this code I found on stackoverflow;
<%form_for :cat_form, :url => {:action => :viewflokkur_selected} do
|f|%>
<%= f.select :cat, @cats_for_mt, {:include_blank => “Flokkar
mælitækis”}, :onchange =>
‘window.location.href = “viewflokkur/” +
this.getAttribute(“value”)’%>
<%end%>
However, “value” appears to be null everytime, so I get thrown to
http://localhost:3000/categories/viewflokkur/null. Is there something I
need to do for this to work, or should I be doing something else? I
don’t really understand how to use observer_fields either, but that
seems to be something I should be looking into, is it not?
Best regards,
Sindri
this.getAttribute(“value”) won’t work on a select input, since the
select doesn’t have a “value” attribute. Rather, it has a list of
options, and one or more of them are selected.
Try this.selectedIndex.value instead.
Tim S. wrote:
this.getAttribute(“value”) won’t work on a select input, since the
select doesn’t have a “value” attribute. Rather, it has a list of
options, and one or more of them are selected.
Try this.selectedIndex.value instead.
Thanks for the suggestion, this.selectedIndex.value does not work
though, now I get
http://localhost:3000/categories/viewflokkur/undefined. Anywhere I can
look this up?
Please post the HTML that is output from the form.
Hi,
This is the output HTML.
Flokkar mælitækis
Almennt
Hjartasjúkraþjálfun
Lungnasjúkraþjálfun
Taugasjúkraþjálfun
Öldrunarsjúkraþjálfun
BR,
Sindri
Finally got it working, used this solution;
<%= select_tag “cat_selected”, options_for_select([‘Flokkar
mælitækis’]+@cats_for_mt),
:onchange => ‘window.location.href = “viewflokkur_from_select/” +
this.selectedIndex’%>
That gave me params[:id] = index-selected, so I created this new method
viewflokkur_from_select wich gets @cats_for_mt[params[:id].to_i-1] and
then redirects to viewflokkur with that value as params[:cat]. Not very
pretty, and a whole lot uglier than I expected, but works…
Thanks everyone for the suggestions!
Hi again,
I’ve also been trying to solve this using observe_field, and that
‘almost’ seems to work. Here’s the code:
<%=select_tag “cat_selected”, options_for_select(@cats_for_mt)%>
<%=observe_field ‘cat_selected’,
:url => {:action => :viewflokkur},
:with => ‘cat’,
:method => :get %>
When I change the value, development.log shows this.
Processing CategoriesController#viewflokkur (for 127.0.0.1 at 2010-06-12
12:04:33) [GET]
Parameters: {“cat”=>“Taugasjúkraþjálfun”,
“authenticity_token”=>“B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=”}
Rendering template within layouts/main
Rendering categories/viewflokkur
Completed in 21ms (View: 18, DB: 0) | 200 OK
[http://localhost/categories/viewflokkur?cat=Taugasjúkraþjálfun&authenticity_token=B2u5ULNr7IJ%2Fta0%2BhiAMBjmjEtTtc%2FyMAQQvSxFn2d0%3D]
So I should be redirected to ‘viewflokkur’, but nothing happens in my
browser. Is there something else I need to do?
BR,
Sindri
Hi,
Try this if you wish to have your code clean :