【问题标题】:nested_form not saving to model Rails 3nested_form 不保存到模型 Rails 3
【发布时间】:2013-04-13 12:21:31
【问题描述】:

我认为我在下面的正确道路上,虽然我无法将表单数据保存到模型中。我有 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"}

表单的当前输出

任何建议表示赞赏

谢谢

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 forms nested-forms model-associations


【解决方案1】:

当您在预测控制器的新方法中将 @fixtures 设置为 Fixture.all 时,您将包括每个灯具,而不仅仅是属于您的预测的灯具。当表单的结果被传递到创建控制器时,有一些与正在传递的其他预测相关联的装置,这是您报告的错误的来源。也许你想要@fixtures = @prediction.fixtures 之类的东西。

你在fields_for 块中所做的事情在我看来也是相当错误的。您将 f.text_field 用于您的 home_scoreaway_score 输入。这将为每个夹具字段中的预测模型重复相同的表单元素。你不会得到你想要的结果。老实说,我很难理解这种关联的意义。你能解释得更好一点吗?我怀疑你的模型没有按照你需要的方式设置。

编辑:

好的,我想我对您现在想要实现的目标有了更好的了解。您正在尝试在一个表格中创建多个预测,并从现有的赛程列表中预先填写 home_team 和 away_team,对吗?

好吧,假设是这样,那么您肯定是以错误的方式接近它。您不需要预测模型和夹具模型之间的多对一关系(正如您正确推测的那样)。您需要做的是通过迭代夹具集合并从当前夹具实例填充home_teamaway_team 字段来生成表单。你真的需要这些是可编辑的文本字段,还是只是将它们放在文本字段中以便它们通过?如果是这样,您可以改用隐藏字段。

现在的问题是 Rails 不允许在一个表单中创建多个记录。这不是我以前做过的事情,我需要进行相当多的反复试验才能使其发挥作用,但这是实现它的一种方法的最佳猜测。

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

所以本质上我是在创建一个返回参数数组的表单。在这种情况下,我无法使用模型表单助手。

解决此问题的一种方法是创建一个具有多个预测的虚拟模型并使用它创建一个嵌套表单。

无论如何,有很多未经测试的代码可能永远不会被查看,所以我暂时将其留在那里。

【讨论】:

  • 嗨@brad,我已经用当前输出的图像更新了问题。我的第一个想法可能是我不需要关联,因为我希望能够对夹具模型中的数据做的就是用夹具填充表单,然后使用其属性创建预测记录。你对此有何看法?创建预测记录后,应该与夹具模型没有任何关系吗?
  • 谢谢布拉德,我会看看,让你知道我是怎么过的,我总是尝试一些事情,所以它永远不会被看:)
  • 好的,所以现在表格是空白的,没有显示得分的固定装置或字段
  • 啊表单标签丢失=,这是在rails 3中出现的>我相信,忘记了这一点,我看到你对隐藏字段做了什么,更好:)所有参数都被传递预测模型也是如此..
  • 我不得不说@brad,我越看越明白,这确实是一个非常好的解决方案,非常干净
【解决方案2】:

我没有花很多时间寻找,因为我的妻子让我很快离开。但是,如果我理解正确的话,您视图中的每一行都来自固定装置和预测模型关联。您得到的参数哈希是一团糟,因为默认行为将是更新单个记录 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 方法中获取更多逻辑,以遍历您收到的哈希并一次保存每一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多