[英]Rails collection_select form adds selected value to an array
I have a Rails 4 app that is a game database. 我有一个Rails 4应用程序,它是一个游戏数据库。 The models relevant to this question are:
与该问题相关的模型是:
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:
我设想它的工作方式如下:
Genre.all
Genre.all
填充的 add_genre
function I've defined to create GameGenre
relationship records for each of the genres. add_genre
保存”时,该应用程序会使用我定义的add_genre
函数为每种流派创建GameGenre
关系记录。 This is my games/new.html.erb
view code at present: 这是我目前的
games/new.html.erb
查看代码:
<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. 这不起作用,因为
fields_for(@game.game_genres.build)
不正确。 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? 我想我需要知道的是,我用什么代替
fields_for(@game.game_genres.build)
将提交的类型传递给新数组? And how can I set up this up in games_controller.rb
? 以及如何在
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). 我意识到
<div><%= hidden_field_tag :game_id, @game.id %></div>
是不必要的(因为在保存新游戏记录之前game_id
不存在)。
I hope I'm making sense, thanks for the help. 我希望我有道理,谢谢您的帮助。
These are my current associations, they are missing accepts_nested_attributes_for
. 这些是我当前的关联,它们缺少
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}) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.