smileymike
smileymike

Reputation: 263

How to display a drop-down list option/selection menu

I have been working on this for few hours and got stuck, so how to get menu selection using database, db using model ZigZagRotation?

<%= form_for([@user, @user.calories_journals.build]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.label :cj_date, "Date Begins:" %>
  <%= f.text_field :cj_date %>
  <%= f.label :no_of_cycles, "Number of Cycles:" %>
  <%= f.text_field :no_of_cycles %>      
  <%= f.label :zig_zag_type, "Zig Zag Rotation Type:" %>
  <%= f.select :zig_zag_type, ZigZagRotation.all.collect {|z| [z.title, z.id ] } %> 
  <%= f.submit "Generate Calories Journals", class: "btn btn-large btn-primary" %>
<% end %>

The line below shows empty details in menu selection box instead of populated list.

<%= f.select :zig_zag_type, ZigZagRotation.all.collect {|z| [z.title, z.id ] } %> 

:zig_zag_type is attr_accessible under ZigZagRotation model and once it is selected I wish the value to be stored in :id.

Upvotes: 0

Views: 657

Answers (1)

gabrielhilal
gabrielhilal

Reputation: 10769

I am assuming that you are working with two different and related models (ZigZagRotation and AnotherModel for example).

If you want to display the zig_zag_type attribute and save its id in the AnotherModel foreign key (zig_zag_id for example), creating the relationship between them, you can do something like the below:

<%= f.collection_select(:zig_zag_id, ZigZagRotation.all, :id, :zig_zag_type , {:include_blank => 'Select Type'} ) %>

You can find more information here.
I hope it helps...

Upvotes: 1

Related Questions