简体   繁体   中英

Ruby select tag options default value

I have an issues with select tag default value. Controller code:

def edit
@folder = Folder.find(params[:id])
@parents = Folder.all.where(:user_id => current_user).map{|u| [ u.name, u.id ]}
end

@parents returns [["tt", 2], ["test", 3], ["test", 4], ["gg", 5], ["test", 1], ["gg", 6], ["tt", 7], ["test 2", 8], ["test 2", 9], ["HH", 10], ["Default", 16], ["Default 33", 17], ["XX", 18]]

My View code:

<%= form_for(:folder, :url => {:action => 'update', :id => @folder.id}) do |f| %>

    <table summary="Folder form fields">
      <tr>
        <th>Name</th>
        <td><%= f.text_field(:name) %></td>
      </tr>
      <tr>
        <th>Parent folder:</th>
        <td>
        <%= f.select(:parent_id, options_for_select(@parents))%></td>
      </tr>

      <%= f.hidden_field :user_id, :value => current_user.id %>
    </table>
...

Select displays folder names correctly, but how to make a default value? I've tried different approaches already, still no result. :selected => value and options_for_select(@parents, value) doesn't work. Please help.

Try this,

This should help you..

<%= f.select :parent_id, options_for_select(@parents,''), {:include_blank => true, :default => nil}, :style=>'**any-inline-css-for-this-select-box**',:onchange=>"any_js_function();"%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM