简体   繁体   中英

Rails collection_select form adds selected value to an array

I have a Rails 4 app that is a game database. The models relevant to this question are:

  • Game
  • Genre
  • GameGenre (the relationship table)

It has a games/new form, which creates new game records.

Within that form, I want a sub-form which allows the user to add genres. I envision it working as follows:

  1. The user selects a genre from the select input. The options are populated from Genre.all
  2. When the user hits the Add button, the id of the selected genre is pushed to an array. They can add as many genres as they want to this array.
  3. When the user hits Save on the main game form, the app uses an add_genre function I've defined to create GameGenre relationship records for each of the genres.

This is my games/new.html.erb view code at present:

<div id="game-genres-form">
  <%= fields_for(@game.game_genres.build) do |f| %>
    <div><%= hidden_field_tag :game_id, @game.id %></div>
    <div class="form-group">
      <%= f.label :genre_id, "Add a genre" %>
      <div class="input-group">
        <%= f.collection_select :genre_id, Genre.all, :id, :name,
                                        {}, {:class=>'form-control'} %>
        <span class="input-group-btn">
          <%= f.submit "Add", class: "btn btn-default" %>
        </span>
    </div>
  <% end %>
</div>

This does not work, because fields_for(@game.game_genres.build) is incorrect. In fact, at present when I click 'Add' it submits the new-game form and creates a game record but doesn't add the genre.

I think what I need to know is, what do I use instead of fields_for(@game.game_genres.build) to pass the submitted genre to a new array? And how can I set up this up in games_controller.rb ?

I realise <div><%= hidden_field_tag :game_id, @game.id %></div> won't be necessary (since game_id doesn't exist until the new game record has been saved).

I hope I'm making sense, thanks for the help.


These are my current associations, they are missing accepts_nested_attributes_for . I'm not too worries about this at the moment - I mainly want to know the right type of form to use, and how to make it add each genre to a hash/array.

class Game < ActiveRecord::Base
  belongs_to :user

  has_many :game_genres,  foreign_key:  :game_id,
                          dependent: :destroy

  has_many :genres, through: :game_genres

class GameGenre < ActiveRecord::Base

  belongs_to :game
  belongs_to :genre

class Genre < ActiveRecord::Base
  belongs_to :user

  has_many :game_genres,  foreign_key:  :genre_id,
                          dependent: :destroy

  has_many :games, through: :game_genres

May be you are missing something like this in your game model

accepts_nested_attributes_for :game_genres ,allow_destroy: true

or you can try this to save your genres in your view

<%= collection_select(:category, :ids, Category.all, :id, :category, { :selected => @categories.map{|m| m.id}}, {:multiple=>true}) %>

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