0

私は Railscast 196 に従っています。関連付けには 2 つのレベルがあります。アプリ -> フォーム -> 質問。これは、フォーム コントローラーの新しいアクションです。

def new
 @app = App.find(params[:app_id])
 @form = Form.new
 3.times {@form.questions.build }
end

ビューには 3 つの質問がすべて正常に表示され、フォームを送信できますが、質問のデータベースには何も挿入されません。これが私の作成アクションです

def create
 @app = App.find(params[:app_id])
 @form = @app.forms.create(params[:form])

 respond_to do |format|
   if @form.save
     format.html { redirect_to(:show => session[:current_app], :notice => 'Form was successfully created.') }
     format.xml  { render :xml => @form, :status => :created, :location => @form }
   else
     format.html { render :action => "new" }
     format.xml  { render :xml => @form.errors, :status => :unprocessable_entity }
   end
 end
end

私の create メソッドに送信されるパラメーターは次のとおりです。

    {"commit"=>"Create Form",
    "authenticity_token"=>"Zue27vqDL8KuNutzdEKfza3pBz6VyyKqvso19dgi3Iw=",
     "utf8"=>"✓",
     "app_id"=>"3",
     "form"=>{"questions_attributes"=>{"0"=>{"content"=>"question 1 text"},
     "1"=>{"content"=>"question 2 text"},
     "2"=>{"content"=>"question 3 text"}},
     "title"=>"title of form"}}`

これは、パラメータが正しく送信されていることを示しています...と思います。質問モデルには「コンテンツ」テキスト列しかありません。

どんな助けでも感謝します:)

4

2 に答える 2

0

わかりました。コンソールをもう少し見ておくべきだったことがわかりました。データベースに質問を挿入しようとしたときにハングアップしていたエラーは、「警告: 保護された属性を一括割り当てできません:questions_attributes」でした。これをアクセス可能な属性に追加すると、うまくいきました。

class Form < ActiveRecord::Base
    belongs_to :app
    has_many :questions, :dependent => :destroy
    accepts_nested_attributes_for :questions
    attr_accessible :title, :questions_attributes
end
于 2011-06-05T22:08:41.087 に答える
0

仮定:

  1. フォームが適切に設定されていること、
  2. サーバーは、データが新しいアクションに送信されていることを示しています。
  3. モデルには、保存をブロックしているコールバックが含まれていません。

変更してみてください:

@form = @app.forms.create(params[:form])

@form = @app.forms.build(params[:form])
于 2011-06-04T07:25:23.767 に答える