has_many relationship,accepts_nested_attributes_for - 未创建嵌套记录

时间:2013-12-29 23:18:58

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试为每个interest_question / feed对创建一个带有score属性的relation_level。我在Feed表单中使用accepts_nested_attributes_for:relation_levels执行此操作。所有内容都应该呈现,并且在提交表单时,会创建一个Feed,但不会创建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

app / models / 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

我已经坚持了一段时间,提前感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

这应该是评论,但它太大了:


也许这可能是你的问题?

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

更清洁,可能有助于您的调试!