I’m working with a form where only entries in the text fields are
being saved to the database. The options selected from the select
lists are not. Not sure what I need to do.
Just to show how things are set up in the controller:
def post @position = Position.new(params[:position]) @position.save
end
Then in the view I’ll show one text field and one select:
<%= form_tag :action => ‘post’ %> #text field
<%= text_field(:position, :title) %> #select
<%= collection_select(:state, liststates, @states, :id, :name, {
:include_blank => true } ) %>
The method liststates is in the helper to gather up the options from the
table.
The intention is to save the state id to the positions table in the
column state_id.
I’m gathering that the select statements need something else.
I’m working with a form where only entries in the text fields are
being saved to the database. The options selected from the select
lists are not. Not sure what I need to do.
Just to show how things are set up in the controller:
def post @position = Position.new(params[:position]) @position.save
end
Then in the view I’ll show one text field and one select:
<%= form_tag :action => ‘post’ %> #text field
<%= text_field(:position, :title) %> #select
<%= collection_select(:state, liststates, @states, :id, :name, {
:include_blank => true } ) %>
collection_select is like all the other similar helpers( such as
text_field) in that if you give it as the first 2 parameters foo and
bar then in the HTML the parameter name is foo[bar], and when the
request is submitter you end up with params[:foo] containing all the
parameters for foo.
Here you are passing :state as the object name, and so in the post
action those settings are found inside params[:state],
Thanks Fred, just had figured out what I need to do, as it’s now
working in one field (one corrected)
Prior - I had a helper that had a bunch of methods such as:
def liststates @states = State.find(:all, :order => “name”)
end
So in my collection select I had to call the method liststates.
Now I tried just throwing @states = State.find(:all, :order => “name”)
in the helper but get a nil error when i called up the form. So I
moved it to the controller action
and then
<%= collection_select(:position, :state_id , @states, :id, :name,
…
with position being the model and state_id the method to update the
table. So I guess I"m good to go.
Stuart
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.