Currently I have a select tag with various options for the State in a from that I have. When validations fail, the page is rendered again and the previously entered values in the textboxes don’t get lost and are still present. But the selected items aren’t retained. How do I code it so it is retained?
View code:
= form_for @user do |f|
= f.text_field :name
= f.select :state, { "California" => "CA", "Texas" => "TX"} , :prompt => ''
= f.submit "Submit"
Controller code:
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_path
else
render 'new'
end
end
Thanks!
Your controller code is good. Your view code is likely the culprit here. Please make sure the forms in your view are consistent.