开发者

Complex Multi Model Rails From

开发者 https://www.devze.com 2023-02-13 23:31 出处:网络
How can I fill in the hidden fields in app/views/rounds/shot_fields.html.erb? app/models/player.rb class Player < ActiveRecord::Base

How can I fill in the hidden fields in app/views/rounds/shot_fields.html.erb?

app/models/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

app/models/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

app/models/shot.rb

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

app/models/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

app/controllers/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

app/views/rounds/_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><开发者_C百科;%= @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 %>

app/views/rounds/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>

app/views/rounds/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>


Passing them in via locals should work.

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

I don't quite understand what you're trying to do (lots of code, not a lot of context), but this is how you pass information into partials.

0

精彩评论

暂无评论...
验证码 换一张
取 消