Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay ... in case anyone was interested I solved the two issues with the following:</p> <ol> <li><p>Used the collect capabilities on the class rather than any simple_form helpers, so the Games View Form Partial had the following changes to the collection line:</p> <pre><code>&lt;%= builder.input :team_id, :collection =&gt; Team.all.collect{ |t| [t.full_name, t.id ]}%&gt; </code></pre></li> <li><p>I used a count comparison to set the veriable noting the value of the hidden field. This is not very object code but means that the code works using simple_form helpers and the controller just uses the .save (in create) and update_attributes (in update) without any further code required. Very nice. Also slightly amended the accepts_nested_attributes_for to look for blank team_id rather than game_id (which would never be blank!). So the code now looks as follows...</p></li> </ol> <p>Game Model:</p> <pre><code> class Game &lt; ActiveRecord::Base belongs_to :venue belongs_to :game_type has_and_belongs_to_many :tournaments has_many :participants, :dependent =&gt; :destroy has_many :teams, :through =&gt; :participants validates :kickoff, :venue_id, :presence =&gt; true accepts_nested_attributes_for :participants, :reject_if =&gt; lambda {|a| a[:team_id].blank? }, :allow_destroy =&gt; :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 </code></pre> <p>Games Controller (New and Create Methods)</p> <pre><code> class GamesController &lt; 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 </code></pre> <p>Games View Form Partial (actually has another partial but included for ease of reading)</p> <pre><code> &lt;%= simple_form_for(@game) do |f| %&gt; &lt;%= f.error_notification %&gt; &lt;div class="form-inputs"&gt; &lt;% count = 0 %&gt; &lt;%= f.simple_fields_for :participants do |builder| %&gt; &lt;% if count == 0 %&gt; &lt;% value = true %&gt; &lt;% count += 1 %&gt; &lt;% else %&gt; &lt;% value = false%&gt; &lt;% end %&gt; &lt;%= builder.input :home_team, :as =&gt; :hidden, :input_html =&gt; {:value =&gt; value} %&gt; &lt;%= builder.input :team_id, :collection =&gt; Team.all.collect{ |t| [t.full_name, t.id ]}%&gt; &lt;% end %&gt; &lt;%= f.input :home_goals, :input_html =&gt; {:maxlength =&gt; 5, :size =&gt; 5 }%&gt; &lt;%= f.input :away_goals, :input_html =&gt; {:maxlength =&gt; 5, :size =&gt; 5 }%&gt; &lt;%= f.input :kickoff %&gt; &lt;%= f.input :completed %&gt; &lt;%= f.input :game_type_id %&gt; &lt;%= f.input :venue_id ,:collection =&gt; Venue.all, prompt: 'Choose the venue'%&gt; &lt;/div&gt; &lt;div class="form-actions"&gt; &lt;%= f.button :submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Hope this is of use to someone. I think it's a good solution but would be interested if anyone has suggestions on improving the logic in the View.</p> <p>Regards</p> <p>Peter</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload