轨道3 - 打造不保存到数据库(Railscast 196) [英] Rails 3 -- Build not saving to database (Railscast 196)

查看:318
本文介绍了轨道3 - 打造不保存到数据库(Railscast 196)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟随着railscast 196我有两个层次的关联。应用程序 - >表 - >问题。这是在形式控制器的新动作。

I'm following along with railscast 196. I've got two levels of associations. App -> Form -> Question. This is the new action in the form controller.

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

该视图显示所有的3个问题很好,我可以提交表单...但没有被插入到数据库中的问题。这是我创造的动作

the view is displaying all 3 questions fine and I can submit the form... but nothing is inserted into the database for the questions. Here is my create action

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

下面是发送到我的创作方法PARAMS:

Here are the params that are sent to my create method:

    {"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"}}`

这表明PARAMS正在发送正确的......我想。问题的模型只是有一个内容文本列。

This shows that the params are being sent correctly... I think. The question model just has a "content" text column.

任何帮助AP preciated:)

Any help appreciated :)

推荐答案

确定了它。原来我一直在寻找我的控制台多一点。这是挂起来的错误尝试插入题到数据库上时,被警告:不能大规模指派保护的属性:questions_attributes。添加此到访问属性并获得成功。

Ok figured it out. Turns out I should have been looking at my console a little more. The error it was hanging up on when trying to insert questions into the db was "Warning: can't mass assign protected attributes :questions_attributes". Adding this into the accessible attributes did the trick.

class Form < ActiveRecord::Base
    belongs_to :app
    has_many :questions, :dependent => :destroy
    accepts_nested_attributes_for :questions
    attr_accessible :title, :questions_attributes
end

这篇关于轨道3 - 打造不保存到数据库(Railscast 196)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆