← Back to Upcase

Get Selected Item From Collection_Select


(Forrest Held) #1

My githup repo is up to date and I have updated the heroku http://glacial-beyond-1616.herokuapp.com/ The github is https://github.com/fheld/mowheels. On the heroku, you will have to register, but there is no checks right now.

I have a collection_select that is populating fine. I just want the following code in my Recipients_Menu view to when the selection box is changed, that it goes to the Menu model and selects the record with the id of the selected menu and brings it back so that the menu object is populated and not nil,

The code looks like this:

<%= form_for(@recipient_menu) do |f| %>
  <% if @recipient_menu.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipient_menu.errors.count, "error") %> prohibited this recipient_menu from being saved:</h2>

      <ul>
      <% @recipient_menu.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :recipient_id %><br>
   <%# f.number_field :recipient_id %>
   <%= f.collection_select :recipient_id, Recipient.all, :id, :first_last_name %>
  </div>

 

  <div class="field">
    <%= f.label :menu_id %><br>
     <%# f.number_field :menu_id %>
   <%= f.collection_select :menu_id, Menu.all, :id, :menu_date  %>
 
  </div>


  <div class="field">
    <%= f.label :main_dish1 %><br>
      <% if @menu %>
        <%= @menu.main_dish1 %>
      <% end %>

I need to find out what the Menu selected was on the collection_select and then go get the record with that ID and bring it back to fill in the menu on the form.


(Derek Prior) #2

I’ve tried to clean up your post with some markdown formatting, but I’m not entirely sure what you’re asking. It sounds like you want to have a series of dynamic select boxes? That is, select something like car “Make, Model” series of select boxes on a car site (changing the make changes the list of available options.

Is this correct?


(Forrest Held) #3

I really don’t want any select boxes connected. I have watched several videos and the railscast on that. I just want that when the user picks a date from the menu date dropdown, the program will go out and get the menu object with that date and make it available… Does that make sense? I want the menu object available.


(Forrest Held) #4

Derek, thank you again. I think you are right. The key is probably in Railscast 88. I just could not draw the correlation. I just need to change the code so that it gets only the one “menu”. I have to use JavaScript for this, am I correct. I kept looking for a way without JavaScript, but it appears to be the best way.