Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Select tag options based on values found in model


  • Please log in to reply
1 reply to this topic

#1 rajend3

rajend3

    Passenger

  • Members
  • 2 posts

Posted 05 April 2014 - 03:08 PM

Hi All,

 

I'm new to rails and I'm not sure how to proceed here.  I have 3 models named User, Program and Enrollment.  User and Program have a many-to-many relationship through Enrollment.  I've created a view to allow users to sign up, but I'm having difficulty understanding how to create a multi-select element that will display all the values found in Program.  This is what I have so far:

<h1>Sign Up</h1>

<%= form_for @user do |user_form| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= user_form.label :first_name %><br />
    <%= user_form.text_field :first_name %>
  </p>

  <p>
    <%= user_form.label :last_name %><br />
    <%= user_form.text_field :last_name %>
  </p>
  
  <p>
    <%= user_form.label :email %><br />
    <%= user_form.text_field :email %>
  </p>

  <p>
    <%= user_form.label :password %><br />
    <%= user_form.password_field :password %>
  </p>

  <p>
    <%= user_form.label :password_confirmation %><br />
    <%= user_form.password_field :password_confirmation %>
  </p>
  
  <%= user_form.fields_for :programs do |program_form| %>
      <%= collection_select(:enrollment, :program_id, Program.all, :id, :name) %>
  <% end %>

  <p class="button"><%= user_form.submit %></p>
<% end %>

Looking at some similar posts I added program_form block, but this just ends up creating an empty select element.  Any help would be appreciated.

 

 



#2 rajend3

rajend3

    Passenger

  • Members
  • 2 posts

Posted 05 April 2014 - 04:49 PM   Best Answer

Solved!

 

Looks like an issue with my program table being empty...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users