复杂的多模型导轨

时间:2011-03-01 15:42:50

标签: ruby-on-rails ruby ruby-on-rails-3 forms

如何填写app / views / rounds / shot_fields.html.erb中的隐藏字段?

应用程序/模型/ player.rb

class Player < ActiveRecord::Base
  has_many :shots, :dependent => :destroy
  belongs_to :team
  belongs_to :user
  has_many :games
  has_and_belongs_to_many :home_games, :class_name => "Game"
  has_and_belongs_to_many :away_games, :class_name => "Game"
end

应用程序/模型/ round.rb

class Round < ActiveRecord::Base
  belongs_to :game, :counter_cache => true
  has_many :shots, :dependent => :destroy
  accepts_nested_attributes_for :shots, :allow_destroy => true
  validates_presence_of :number
  validates_numericality_of :number
end

应用程序/模型/ shot.rb

class Shot < ActiveRecord::Base
  belongs_to :player, :counter_cache => true
  belongs_to :game
  belongs_to :round
  belongs_to :team
end

应用程序/模型/ game.rb

class Game < ActiveRecord::Base
  has_many :shots, :dependent => :destroy
  has_many :rounds, :order => 'number', :dependent => :destroy
  accepts_nested_attributes_for :shots
  belongs_to :away, :class_name => 'Team'
  belongs_to :home, :class_name => 'Team'

  has_and_belongs_to_many :home_players, :class_name => 'Player', :association_foreign_key => "home_player_id"
  has_and_belongs_to_many :away_players, :class_name => 'Player', :association_foreign_key => "away_player_id"

  accepts_nested_attributes_for :rounds, :allow_destroy => true
end

应用程序/控制器/ rounds_controller.rb

def new
    @game = Game.find(params[:game_id])
    @round = @game.rounds.build
    @round.number = @game.rounds.count > 1 ? @game.rounds.count + 1 : 1
  end

应用程序/视图/发/ _form.html.erb

<% if @round.errors.any? %>
  <div class="error">
    <% @round.errors.full_messages.each do |msg| %>
      <%= msg %><br/>
    <% end %>
  </div>
<% end %>

<%= form_for @game do |f| %>
  <%= field_set_tag "Rounds" do %>
    <table class="sortable">
      <thead>
        <tr>
          <th>Number</th>
          <th><%= @game.away_players[0].name %></th>
          <th><%= @game.away_players[1].name %></th>
          <th><%= @game.away_players[2].name %></th>
          <th><%= @game.home_players[0].name %></th>
          <th><%= @game.home_players[1].name %></th>
          <th><%= @game.home_players[2].name %></th>
          <th>Remove</th>
        </tr>
      </thead>
      <tbody>
        <%= f.fields_for :rounds do |round_form| %>
            <%= render 'round_fields', :f => round_form %>
        <% end -%>
      </tbody>
    </table>

    <p>
      <%= link_to_add_fields "Add Round", f, :rounds %>
    </p>

    <p>
      <%= f.submit %>
    </p>
  <% end %>
<% end %>

应用程序/视图/发/ round_fields.html.erb

<% 6.times { f.object.shots.build } if f.object.new_record? -%>
<tr>
  <td>
    <%= f.text_field :number, :size => 3 %>
  </td>

  <%= f.fields_for :shots do |shot_form| %>
    <%= render 'shot_fields', :f => shot_form %>
  <% end -%>

  <td>
    <%= f.check_box(:_destroy) %>
    <%= f.hidden_field :id %>
  </td>
</tr>

应用程序/视图/发/ shot_fields.html.erb

<td>
  <%= f.select :cup, [["Miss", 0], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ["No Shot", ""], ["Suicide", 11]] %>
  <%= f.hidden_field :id %>
  <%= f.hidden_field :game_id, :value => params[:id] %>
  <%# f.hidden_field :player_id, :value => player.id %>
  <%# f.hidden_field :team_id, :value => team.id %>
</td>

1 个答案:

答案 0 :(得分:1)

通过locals传递它们应该有效。

<%= render 'shot_fields', :locals => { :f => shot_form, :player => some_player_you_dont_have_defined, :team => some_team_variable_i_dont_see } %>

我不太明白你要做什么(很多代码,而不是很多上下文),但这就是你将信息传递给部分内容的方式。