0

我已经阅读了几个线程,到目前为止还没有。我正在尝试将一种形式嵌套在另一种形式中。我收到无法批量分配受保护的属性错误。\

app/controllers/projects_controller.rb:46:in new' app/controllers/projects_controller.rb:46:increate'

Projects_controller.rb

 def create
  @project = Project.new(params[:project])


respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render json: @project, status: :created, location: @project }
  else
    format.html { render action: "new" }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
 end
end

项目.rb

  WF::Application.routes.draw do
    resources :line_items


    resources :projects do
      resources :line_items
    end

   devise_for :users
   get 'about' => 'pages#about'
   get 'Production' => 'projects#index'
   root :to => 'pages#home'
end

这是错误...
ProjectsController#create 中的 ActiveModel::MassAssignmentSecurity::Error

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

这是我的项目模型

class Project < ActiveRecord::Base
    attr_accessible :quantity
    # may be unnessary
    attr_accessible :line_items_attributes


    belongs_to :user
    has_many :line_items
    accepts_nested_attributes_for :line_items, :allow_destroy => true
end
4

2 回答 2

0

Rails 试图保护您避免意外分配您无意分配的值。

您可以通过这种方式告诉 Rails 可以分配哪些值:

attr_accessible :value1, :value2

如果您将该行添加到Project 模型的顶部(替换:value1:value2使用列的实际名称),它应该允许您执行您正在尝试的操作。

有关更多信息,请参阅文档

于 2013-04-17T01:52:22.113 回答
0

假设您尝试通过项目模型创建行项目,您需要确保Project模型中有以下行:

# project.rb
Class Project < ActiveRecord::Base
  attr_accessible :line_items_attributes

  accepts_nested_attributes_for :line_items

  ...
end
于 2013-04-17T02:32:00.893 回答