Hi everyone, i have the following code:
<%= form_tag :action => ‘list_wedding_by_continent’, :id => “Africa” %>
<%= collection_select(:thing, :continent, @continent,“id”,“continent”)
<%= submit_tag ‘Search’ %>
<%= end_form_tag %>
how do i pass in the selected country in the list, instead of hard
Forget the rails helpers for a moment and go back to HTML basics.
You’ve got a form (which rails can help you create using form_tag, or
even better, form_for). This submits a post request to a URL (which in
the rails helper can be generated with a hash of parameters, including
perhaps :action => ‘some_action’). The contents of that form (including,
as in your case, a select box, which rails can help you create with
select, select_tag, collection_select, etc) are passed to the receiving
URL for it to do with what it wants.
select box can end up the url the form is submitting to, which I think
is what you’re asking. Have a play around with the helpers and examine
the source code they produce in your browser, or check out the API. It
will all become clear… eventually.
Thanks Chris T, and i really appreciate you taking the time to explain
it to me. But i am still confused, if there is no way to get the
information out of a form, whats the point of it?
You should get the selected continent coming through as things are (just
inspect the contents of the params hash that is passed to your
However I think you’re expecting params[:id] to contain the selected
This won’t happen because
- I think that having :id => ‘Africa’ in form_tag will effectively mean
that it’s overriden
- collection_select (like the various other helpers of its family) will
put the selected parameter in params[:thing][:continent] (Check out the
html that is generated by your form
Great, thanks Fred - sorted it.