问题描述
|
我跟着railscast196。我有两个级别的关联。应用->表格->问题。这是表单控制器中的新动作。
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\"}}`
这表明参数已正确发送...我认为。问题模型只有一个“内容”文本列。
任何帮助表示赞赏:)
解决方法
假设:
您的表格设置正确,
您的服务器显示您的数据正在发送到新操作,并且
您的模型不包含阻止保存的回调,
尝试更改:
@form = @app.forms.create(params[:form])
至
@form = @app.forms.build(params[:form])
, 确定了。原来,我应该多看看我的控制台。尝试向数据库中插入问题时挂起的错误是“警告:无法批量分配受保护的属性: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