Ruby on rails 3 Rails 3.2.2简单表单嵌套模型(有很多)可以工作,但需要改进
使用Rails 3.2.2中的simple_forms gem,我构建了一个表单来创建嵌套模型中的对象(railscasts.com上的Ryan Bates再次对此表示赞许),但我正在努力掌握一种多通关系的复杂性,而无需为我的游戏控制器的创建方法添加额外的代码 我正在使用accepts_nested_attributes_for:我的游戏模型中的参与者,希望能够批量分配2个必需的参与者对象 我有三门课:一场游戏,一个团队,和一个全桌参与者 参与者的存在使一支球队在比赛中可以被称为主队或客队 目前,我正在使用表单中的select_标记创建两个psuedo参数,在保存新的@game对象后,我将在控制器中使用create方法创建参与者。我正在为主队和客队创建@game.participant。这一切都是可行的,但有点过于繁重,如果我想创建一个游戏锦标赛而不编写更多的控制器代码,则无法扩展 因此,现在我在新方法中构建了2.5倍的参与者,并尝试在不添加额外代码和删除create@games.participants的情况下让create和update正常工作 好的。。。问题是Ruby on rails 3 Rails 3.2.2简单表单嵌套模型(有很多)可以工作,但需要改进,ruby-on-rails-3,collections,nested-forms,simple-form,Ruby On Rails 3,Collections,Nested Forms,Simple Form,使用Rails 3.2.2中的simple_forms gem,我构建了一个表单来创建嵌套模型中的对象(railscasts.com上的Ryan Bates再次对此表示赞许),但我正在努力掌握一种多通关系的复杂性,而无需为我的游戏控制器的创建方法添加额外的代码 我正在使用accepts_nested_attributes_for:我的游戏模型中的参与者,希望能够批量分配2个必需的参与者对象 我有三门课:一场游戏,一个团队,和一个全桌参与者 参与者的存在使一支球队在比赛中可以被称为主队或客队 目前
class Game < ActiveRecord::Base
belongs_to :venue
belongs_to :game_type
has_and_belongs_to_many :tournaments
has_many :participants, :dependent => :destroy
has_many :teams, :through => :participants
validates :kickoff, :venue_id, :presence => true
accepts_nested_attributes_for :participants,
:reject_if => lambda {|a| a[:game_id].blank? },
:allow_destroy => :true
def home_team
self.teams.joins(:participants).where("participants.home_team = ?", true).first
end
def away_team
self.teams.joins(:participants).where("participants.home_team = ?", false).first
end
end
class Game < ActiveRecord::Base
belongs_to :venue
belongs_to :game_type
has_and_belongs_to_many :tournaments
has_many :participants, :dependent => :destroy
has_many :teams, :through => :participants
validates :kickoff, :venue_id, :presence => true
accepts_nested_attributes_for :participants,
:reject_if => lambda {|a| a[:team_id].blank? },
:allow_destroy => :true
def home_team
self.teams.joins(:participants).where("participants.home_team = ?", true).first
end
def away_team
self.teams.joins(:participants).where("participants.home_team = ?", false).first
end
end
类游戏:销毁
有许多:团队,:至=>:参与者
验证:启动,:地点\ id,:状态=>true
接受:参与者的\u嵌套\u属性\u,
:reject_if=>lambda{a | a[:game_id].blank?},
:allow_destroy=>:true
def home_团队
self.teams.joins(:参与者)。其中(“participants.home\u team=?”,true)。首先
结束
德福客队
self.teams.joins(:参与者)。其中(“participants.home\u team=?”,false)。首先
结束
结束
参与者模型:
class Participant < ActiveRecord::Base
belongs_to :game
belongs_to :team
end
class参与者
团队模式:
class Team < ActiveRecord::Base
has_many :participants, :dependent => :destroy
has_many :games, :through => :participants
validates :full_name, :presence => true
validates :short_name, :presence => true, :length => { :within => 1..5 }
end
class团队:销毁
有许多:游戏,:至=>:参与者
验证:全名,:presence=>true
验证:short_name,:presence=>true,:length=>{:within=>1..5}
结束
游戏控制器(新建和创建方法)
class GamesControllerparams[:home],:home_team=>true)
@game.participants.create(:team_id=>params[:away],:home_team=>false)
format.html{重定向到游戏路径,注意:'游戏已成功创建。}
其他的
format.html{呈现操作:“新建”}
结束
结束
结束
游戏视图表单部分(实际上有另一部分,但为了便于阅读而包括在内)
{:maxlength=>5,:size=>5}%>
{:maxlength=>5,:size=>5}%>
vention.all,提示:“选择场地”%>
好的。。。如果有人感兴趣,我用以下方法解决了这两个问题:
<%= builder.input :team_id, :collection => Team.all.collect{ |t| [t.full_name, t.id ]}%>
Team.all.collect{| t |[t.full|u name,t.id]}%>
class Game < ActiveRecord::Base
belongs_to :venue
belongs_to :game_type
has_and_belongs_to_many :tournaments
has_many :participants, :dependent => :destroy
has_many :teams, :through => :participants
validates :kickoff, :venue_id, :presence => true
accepts_nested_attributes_for :participants,
:reject_if => lambda {|a| a[:game_id].blank? },
:allow_destroy => :true
def home_team
self.teams.joins(:participants).where("participants.home_team = ?", true).first
end
def away_team
self.teams.joins(:participants).where("participants.home_team = ?", false).first
end
end
class Game < ActiveRecord::Base
belongs_to :venue
belongs_to :game_type
has_and_belongs_to_many :tournaments
has_many :participants, :dependent => :destroy
has_many :teams, :through => :participants
validates :kickoff, :venue_id, :presence => true
accepts_nested_attributes_for :participants,
:reject_if => lambda {|a| a[:team_id].blank? },
:allow_destroy => :true
def home_team
self.teams.joins(:participants).where("participants.home_team = ?", true).first
end
def away_team
self.teams.joins(:participants).where("participants.home_team = ?", false).first
end
end
类游戏:销毁
有许多:团队,:至=>:参与者
验证:启动,:地点\ id,:状态=>true
接受:参与者的\u嵌套\u属性\u,
:reject_if=>lambda{a | a[:team_id].blank?},
:allow_destroy=>:true
def home_团队
self.teams.joins(:参与者)。其中(“participants.home\u team=?”,true)。首先
结束
德福客队
self.teams.joins(:参与者)。其中(“participants.home\u team=?”,false)。首先
结束
结束
游戏控制器(新建和创建方法)
class GamesCon
class GamesController < ApplicationController
def new
@game = Game.new
2.times { @game.participants.build }
respond_to do |format|
format.html # new.html.erb
end
end
def create
@game = Game.new(params[:game])
respond_to do |format|
if @game.save
format.html { redirect_to games_path, notice: 'Game was successfully created.' }
else
format.html { render action: "new" }
end
end
end
end
<%= simple_form_for(@game) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% count = 0 %>
<%= f.simple_fields_for :participants do |builder| %>
<% if count == 0 %>
<% value = true %>
<% count += 1 %>
<% else %>
<% value = false%>
<% end %>
<%= builder.input :home_team, :as => :hidden, :input_html => {:value => value} %>
<%= builder.input :team_id, :collection => Team.all.collect{ |t| [t.full_name, t.id ]}%>
<% end %>
<%= f.input :home_goals, :input_html => {:maxlength => 5, :size => 5 }%>
<%= f.input :away_goals, :input_html => {:maxlength => 5, :size => 5 }%>
<%= f.input :kickoff %>
<%= f.input :completed %>
<%= f.input :game_type_id %>
<%= f.input :venue_id ,:collection => Venue.all, prompt: 'Choose the venue'%>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>