Search code examples
ruby-on-railsdrop-down-menuformhelper

Rails, how to pass selected value from a dropdown list as same as other values in the form


I have a form for users to upload pictures with some other informations like name and description. I have two databases tables: one is albums and the other is photos. In photos table there is a field called album_id which connects the photo to the album.

<%= form_for(@photo, :html => { :multipart => true }) do |f| %>
  <div>
    <div class='form_text'> Add Photo </div>
    <%= f.file_field :image %>
  </div>
  <div>
    <div class='form_text'> Name </div>
    <%= f.text_field :name %>
  </div>
  <div>
    <div class='form_text'> Description </div>
    <%= f.text_field :description %>
  </div>
  <div>
    <%= collection_select(:album, :album_id, Album.all, :id, :name, :prompt => true)  %>
  </div>
  <div>
    <%= f.submit 'Upload' %>
  </div>

So my problem is that how I can pass the selected value of album_id as same as other values. Maybe I am not clear enough, I think the selection is not added into the f object, so when the value passes when click on Upload, name, description and the photo are passed in as one object and the album_id is passed as another object. How can make the selection into the f object?


Solution

  • I think it should be

    <%= collection_select(:photo, :album_id, Album.all, :id, :name, :prompt => true) %>
    

    So it would generate a select with the id photo[album_id], as your other fields (doc).