johann
johann

Reputation: 1135

Rails 4.0 : Selected value for form_for [select]

When submitting the below form, parameters are available in the controller with params[:teachers], but in the view (same view is rendered), selected value for "language_id" isn’t params[:teacher][:language_id], but the default one (first option).

<%= form_for :teacher , :url => {:action =>"search_teacher"} , :html => { :method => "post"} do | f | %>
<%= f.select :language_id , t('languages_hash'), :include_blank => false  %>
<%= f.submit  :value => t("search_button") %>

When debugging with the debug method in the view, « params[:teacher][:language_id] » parameter is present.

Thank you

Upvotes: 4

Views: 7430

Answers (2)

Deepak A
Deepak A

Reputation: 322

By what I understood from your question, you are trying to get the previously selected language to remain selected after the form submission. For that you may need to pass a selected option in the f.select.

<%= f.select("Language", "language_id", Language.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true, :selected => params[:language_id] }) %>

This may do the trick.

For more option you can go through this link

Upvotes: 4

CDub
CDub

Reputation: 13354

I'm guessing that t('languages_hash') isn't returning data in the proper format that the select tag is expecting.

Check out the docs - I'm guessing you'll want to wrap that in an options_for_select call, and possibly change the order of what is returned from t('languages_hash').

Upvotes: 1

Related Questions