Search code examples
ruby-on-railsruby-on-rails-3formsnested-formsmodel-associations

nested_form not saving to model Rails 3


I think I am on the right path for the following, though i cannot save the form data to the model. I have 2 models

class Prediction < ActiveRecord::Base
  attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes

  has_many :fixtures
  accepts_nested_attributes_for :fixtures
end

class Fixture < ActiveRecord::Base
  attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time

  belongs_to :predictions
end

To create a new prediction record i have a form that takes all the fixtures and pre populates the form and the user will just add scores next to each team

<%= form_for @prediction do |f| %>
<!-- Gets all fixtures -->
<%= f.fields_for :fixtures, @fixtures<!-- grabs as a collection --> do |ff| %>

<%= ff.text_field :home_team %> VS <%= ff.text_field :away_team %><%= f.text_field :home_score %><%= f.text_field :away_score %><br>

<% end %>
<%= f.submit 'Submit Predictions' %>
<% end %>

Then i have my controller to take care of the new/create action, which i think is where i may be falling over

class PredictionsController < ApplicationController

def new
 @prediction = Prediction.new
 @prediction.fixtures.build
 @fixtures = Fixture.all
end

 def create
  @prediction = Prediction.new(params[:prediction])
  @prediction.save
   if @prediction.save
    redirect_to root_path, :notice => 'Predictions Submitted Successfully'
   else
    render 'new'
end
  end
 end

and finally my routes

resources :predictions
resources :fixtures

So when i submit the form i get the error

ActiveRecord::RecordNotFound in PredictionsController#create
Couldn't find Fixture with ID=84 for Prediction with ID=

Looking at the params being parsed (snapshot below), something does not look right, for one the home_score and away_score are not being passed through.

{"utf8"=>"✓",
 "authenticity_token"=>"DfeEWlTde7deg48/2gji7zSHJ19MOGcMTxEsQEKdVsQ=",
  "prediction"=>{"fixtures_attributes"=>{"0"=>{"home_team"=>"Arsenal",
 "away_team"=>"Norwich",
 "id"=>"84"},
 "1"=>{"home_team"=>"Aston Villa",
 "away_team"=>"Fulham",
 "id"=>"85"},
 "2"=>{"home_team"=>"Everton",
 "away_team"=>"QPR",
 "id"=>"86"}

Current Output of form

image

Any advice appreciated

Thanks


Solution

  • When you set @fixtures to Fixture.all in the new method in your prediction controller, you are including every fixture, not just the fixtures belonging to your prediction. When the results of the form are passed to the create controller there are fixtures associated with other predictions being passed in which is the source of the error you have reported. Perhaps you want something like @fixtures = @prediction.fixtures.

    What you are doing in the fields_for block also looks fairly wrong to my eyes. You are using f.text_field for your home_score and away_score inputs. This will repeat the same form element for the prediction model in each fixture field. You won't get the result you want from this. To be honest, I'm struggling to understand how this association makes sense. Are you able to explain it a little better? My suspicion is that your models are not quite set up the way you need them to be.

    edit:

    OK, I think I have a better idea of what you're trying to achieve now. You're trying to create many predictions in one form and prefill the home_team and away_team from a list of existing fixtures, right?

    Okay, assuming that is so, you're definitely approaching it the wrong way. You don't need a many-to-one relationship between your prediction and fixture models (as you have correctly surmised). What you need to do is generate your form by iterating over the collection of fixtures and populating the home_team and away_team fields from the current fixture instance. Do you actually need these to be editable text fields, or are you just putting them in a text field so they get passed through? If so, you could use hidden fields instead.

    The problem now though, is that Rails doesn't easily allow creating multiple records in the one form. It's not something I've done before and it would take me quite a bit of trial-and-error to make it work, but here's a best guess for one way of making it so.

    models
    
    class Prediction < ActiveRecord::Base
      attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes
    end
    
    class Fixture < ActiveRecord::Base
      attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time
    end
    

    controller
    
    class PredictionsController < ApplicationController
    
      def new
        @prediction = Prediction.new
        @fixtures = Fixture.all
      end
    
      def create
        begin
          params[:predictions].each do |prediction|
            Prediction.new(prediction).save!
          end
          redirect_to root_path, :notice => 'Predictions Submitted Successfully'
        rescue
          render 'new'
        end
      end
    
    end
    

    view
    
    <%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
      <% @fixtures.each do |fixture| %>
        <%= fixture.home_team %> vs <%= fixture.away_team %>
        <%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
        <%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
        <%= text_field_tag "predictions[][home_score]" %>
        <%= text_field_tag "predictions[][away_score]" %><br />
      <% end %>
      <%= submit_tag "Submit predictions" %>
    <% end %>
    

    So essentially I'm creating a form that returns an array of parameters. I can't use the model form helpers in this situation.

    One way around this would be to create a dummy model that has_many predictions and create a nested form using this.

    Anyway, that's a lot of untested code that may never get looked at so I'm going to leave it there for now.