1

我试图让嵌套表单工作,但不断收到以下错误:

ActiveModel::MassAssignmentSecurity::ResponsesController#create 中的错误

无法批量分配受保护的属性:gate_answer

这是我的模型......我做错了什么?

class Response < ActiveRecord::Base
  attr_accessible :gate_id, :gate_answers_attributes
  belongs_to :gate
  has_many :gate_answers
  accepts_nested_attributes_for :gate_answers
end

class GateAnswer < ActiveRecord::Base
  attr_accessible :prospect_id, :gate_question_id, :content, :response_id
  belongs_to :response
end

和数据库模式提取:

create_table "gate_answers", :force => true do |t|
  t.integer  "prospect_id"
  t.integer  "gate_question_id"
  t.text     "content"
  t.datetime "created_at",                      :null => false
  t.datetime "updated_at",                      :null => false
  t.integer  "response_id"
end

create_table "responses", :force => true do |t|
  t.integer  "gate_id"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
end

..和我的控制器

# responses_controller.rb

def create
  @response = Response.new(params[:response])

  respond_to do |format|
    if @response.save
      format.html { redirect_to :action => 'index', :gate_id => (params[:response][:gate_id]), notice: 'Response was successfully created.' }
    else
      format.html { render action: "new" }
    end
  end
end


#gate_answers_controller.rb

def create

@gate_answer = GateAnswer.new(params[:gate_answer])

#code below finds URL that user will be redirected to after form is saved
@gate = Gate.find(params[:gate_answer][:gate_id])

  respond_to do |format|
    if @gate_answer.save
      format.html { redirect_to @gate.content_url, notice: 'Redirecting.' }  #redirect to URL per above
    else
      format.html { render action: "new" }
    end
  end
end

我究竟做错了什么?

4

2 回答 2

0

Railscasts嵌套模型表单第 1部分和第 2部分详细介绍了创建这种类型表单的方法。也许这会对你有所帮助?

于 2013-01-21T14:21:57.470 回答
0

从这里尝试表单对象方法http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

于 2013-01-21T12:36:35.043 回答