![](/img/trans.png)
[英]accepts_nested_attributes_for with has_many => :through Options
[英]has_many relationship, accepts_nested_attributes_for - nested record not created
我正在尝试为每个interest_question / feed对创建一个带有score属性的Relation_level。 我正在使用Feed表单中的accepts_nested_attributes_for:relation_levels进行此操作。 一切都会按其应有的方式呈现,并且在提交表单时,会创建一个提要,但不会创建relation_levels。
我也尝试过将feed_id添加为表单中的隐藏字段。
(使用rails 4和haml)
应用程序/模型/ interest_question.rb
class InterestQuestion < ActiveRecord::Base
has_many :relation_levels, dependent: :destroy
has_many :interest_answers, dependent: :destroy
end
应用程序/模型/ relation_level.rb
class RelationLevel < ActiveRecord::Base
belongs_to :feed
belongs_to :interest_question
end
应用程序/模型/ feed.rb
class Feed < ActiveRecord::Base
has_many :relation_levels, dependent: :destroy
has_many :interest_questions, through: :relation_levels
accepts_nested_attributes_for :relation_levels
end
应用程序/视图/供稿/ _form.html.haml
=form_for(@feed) do |f|
...other fields
...other fields
-@interest_questions.each do |iq|
=f.fields_for @feed.relation_levels.build(interest_question_id: iq.id) do |rl|
=rl.label iq.question_text
=rl.range_field :score, max: 100, min: 0, default: 0
=f.submit
应用程序/控制器/ feeds_controller.rb
class FeedsController < ApplicationController
def new
@feed = Feed.new
@sources = Source.all
@interest_questions = InterestQuestion.all
end
def create
@feed = Feed.new(feed_params)
if @feed.save
redirect_to '/feeds', notice: 'Feed created.'
else
render action: 'new'
end
end
...
private
def feed_params
params.require(:feed).permit(..., relation_levels_attributes:
[:interest_question_id, :score, :feed_id])
end
服务器输出:
Started POST "/feeds" for 127.0.0.1 at 2013-12-30 15:00:58 -0600 Processing by FeedsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"/+TOOdpZZk85YvVlxkRIpLNfPfVVtGUTlKPb9Ctkvh8=", "feed"=>{"url"=>"lkas6df.com", "source_id"=>"1", "section"=>"kasl6d6fa", "area_importance"=>"", "is_local_news"=>"0", "relation_level"=>{"score"=>"17"}}, "commit"=>"Create Feed"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '6fcabf7c7b1376250b1ffa589ff4f2279854d066' LIMIT 1 Unpermitted parameters: relation_level (0.1ms) begin transaction Source Load (0.1ms) SELECT "sources".* FROM "sources" WHERE "sources"."id" = ? ORDER BY "sources"."id" ASC LIMIT 1 [["id", 1]] Feed Exists (0.2ms) SELECT 1 AS one FROM "feeds" WHERE "feeds"."url" = 'lkas6df.com' LIMIT 1 SQL (1.7ms) INSERT INTO "feeds" ("created_at", "section", "source_id", "updated_at", "url") VALUES (?, ?, ?, ?, ?) [["created_at", Mon, 30 Dec 2013 21:00:58 UTC +00:00], ["section", "kasl6d6fa"], ["source_id", 1], ["updated_at", Mon, 30 Dec 2013 21:00:58 UTC +00:00], ["url", "lkas6df.com"]] (170.8ms) commit transaction Redirected to http://localhost:3000/feeds
我已经坚持了一段时间,在此先感谢您的帮助:)
这应该是一条评论,但太大了:
也许这可能是您的问题?
=f.fields_for @feed.relation_levels.build(interest_question_id: iq.id) do |rl|
这会动态生成一个ActiveRecord对象,这是IMO的不良做法。 我将在控制器中构建对象,然后在f.fields_for
调用该对象,如下所示:
#app/controllers/feeds_controller.rb
def new
@feed = Feed.new
@sources = Source.all
@interest_questions = InterestQuestion.all
@interest_questions.count.times do
@feed.relation_levels.build
end
end
然后,您可以致电:
=f.fields_for :relation_levels do |rl|
更清洁,可能会帮助您调试!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.