I have a dropdown list ready to go on my rails app. It calls my
action. My action works. Everythings good. Except I can't figure out
for the life of me how to pass the value selected in the dropdown list
as a parameter to the action. I've discovered that forms are involved,
though no matter how many example I go through I can't get them to
work. Here's what I've got:
The dropdown list in the view:
<% form_tag({:controller => 'remote_site_controller',
:action => 'filter'}) do %>
<%= text_field_tag(:search_options, params[:search_options])%>
<%= submit_tag("Display text!")%>
<% end -%>
<form id="filter_form" name="filter_form" action="filter"
method="POST">
<td class="option">
<select name="filterlist[name]">
<option value="">Choose Filter Type
<% @filter_lists.each do |filterlist| %>
<option value="<%= filterlist.id %>"
<%= ' selected' if filterlist.id == @filter_lists.id
%>>
<%= filterlist.name %>
</option>
<% end %>
</select>
def filter
type = params[:search_options]
render_text type
end
I've commented everything out of my action at the moment, and threw in
the render_text just to see what comes up. So far, for every attempt
I've made, render_text gives me nothing (leaving me to assume that
type is empty and not receiving the parameter). If you couldn't tell,
I've never used a form before so I'm running around in the dark, for
the most part. If anyone could drop me a hint I'd greatly appreciate
it, thanks.
I have a dropdown list ready to go on my rails app. It calls my
action. My action works. Everythings good. Except I can't figure out
for the life of me how to pass the value selected in the dropdown list
as a parameter to the action. I've discovered that forms are involved,
though no matter how many example I go through I can't get them to
work. Here's what I've got:
You've got two forms and your select is not in the form that you're
submitting (rails has helpers for creating select tags by the way)
Then everything below would fall into my :search_options form?
You could leave it if you want, it doesn't matter. the important thing
is that any input you want submitted should be contained in the first
form (and that includes the submit button, or else it will still be
submitting the second form)
And when I click the button, nothing happens. So somehow my action
isn't being called... Or the button/dropdown isn't included in the
form.
The button is outside the form (assuming you're talking about the
Filter button rather than the 'Display text' one), therefore clicking
it does not submit the form. It probably doesn't help that your html
is completely malformed (which is probably why the first version
didn't work). In no particular orcer you've got a <td> which you never
close (which sort of implies that the form above is at the top level
of the form, which is also not allowed). You don't close your option
tags. The w3c html validator should be able to help you there (and I'd
encourage you to read up on at least some basic html as you seem to be
flailing around in the dark). You might save yourself some grief by
using the rails helpers to generate your select box.
Gross. Alright, last try... is there anyway to pass the parameter (the
selected value in the dropdown list) to the controller just by using
this form? :
<form id="filter_form" name="filter_form" action="filter"
method="POST">
<td class="option">
<select name="filterlist[name]">
<option value="">Choose Filter Type
<% @filter_lists.each do |filterlist| %>
<option value="<%= filterlist.id %>"
<%= ' selected' if filterlist.id == @filter_lists.id
%>>
<%= filterlist.name %>
</option>
</option>
<% end %>
</select>
Gross. Alright, last try... is there anyway to pass the parameter (the
selected value in the dropdown list) to the controller just by using
this form? :
You've still got syntactically invalid stuff there (and depending
where this file lives, action="filter" might be trying to call the
filter action on a different controller).
You also can't have a <td> tag inside a form like that. A td can only
be contained within a tr.
The <tr> and </tr> aren't contained in the fragment I pasted, but
they're there, don't worry. This code works, I've tested it. I just
don't know if its possible to pass a parameter with it. In the
controller I've tried
@type = params[:filter_form] and
@type = params["filter_form"]
but neither works. I've tested enough to know the action is being
called. Every example I've seen (containing forms) passes a parameter
with a rails form, which leads me to believe I need to use one, and
not the html form I've pasted above, to pass the value.
The <tr> and </tr> aren't contained in the fragment I pasted, but
they're there, don't worry. This code works, I've tested it. I just
don't know if its possible to pass a parameter with it. In the
controller I've tried
You've not understood me: a td tag's parent be contained a tr tag.
Your's is a form tag
@type = params[:filter_form] and
@type = params["filter_form"]
but neither works. I've tested enough to know the action is being
called. Every example I've seen (containing forms) passes a parameter
with a rails form, which leads me to believe I need to use one, and
not the html form I've pasted above, to pass the value.
rails forms and html forms are the same thing. the rails form helpers
just generate regular form tags.
the parameter would be available as params[:filterlist][:name] if it
were passed, but like I said the html is so mangled the browser could
be forgiven for not going it.
You can see all the parameters passed in development.log (or stick a
breakpoint in your action) rather than trying to guess what might be
there.
The webpage works fine. I host is locally and open it in a web
browser. Presto, it pops up, dropdown, button and all. The only thing
that doesn't work is passing the parameter.
So if I have <select name="filterlist[name]">
then the name of the parameter is filterlist[name]?
So then in my controller I should do something like:
@type = params["filterlist[name]"]
(I was using "filter_form" because thats the id of the form. Got to
identify it somehow, I thought that was how you did it)
Thanks for the tip with the "GET" method. Tried that, the correct
parameter showed up in the address bar, so I know its there. Just need
to nab it.
Eureeka!
params[:filterlist][:name] worked like a charm.
Thanks for sticking through it with me guys. And thanks for putting up
with me and my horrible html skills. I think I'm going to go throw a
party.