0

我认为我走在正确的道路上,尽管我无法将表单数据保存到模型中。我有 2 个模型

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

为了创建一个新的预测记录,我有一个表格,它包含所有的装置并预先填充表格,用户只需在每个团队旁边添加分数

<%= 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 %>

然后我让我的控制器来处理新/创建操作,我认为这是我可能会摔倒的地方

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

最后是我的路线

resources :predictions
resources :fixtures

所以当我提交表单时,我得到了错误

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

查看正在解析的参数(下面的快照),有些东西看起来不正确,因为 home_score 和 away_score 没有通过。

{"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"}

当前输出形式

图片

任何建议表示赞赏

谢谢

4

2 回答 2

1

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.

于 2013-04-13T14:18:03.960 回答
0

我没有花很多时间寻找,因为我的妻子让我很快离开。但是,如果我理解正确的话,您视图中的每一行都来自固定装置和预测模型关联。您得到的参数哈希是一团糟,因为默认行为将是更新单个记录 ID 的视图。因此,您将多条记录带入一个视图并尝试一次更新多条记录。

您在模型中的创建方法

@prediction = Prediction.new(params[:prediction])

但是您传递的参数哈希是这样的:

{"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"}

因此,您必须在预测控制器中的 create 方法中获取更多逻辑,以遍历您收到的哈希并一次保存每一行。

于 2013-04-13T21:52:16.237 回答