我看过其他帖子,看不出我做错了什么。 我收到了这个错误:
ActiveModel::MassAssignmentSecurity::Error in ProjectsController#create
Can't mass-assign protected attributes: blog_post
这是我的两个型号和我的表格。我的印象是,项目控制器中没有任何特殊需要。项目或blog_post控制器中有什么东西可以解决这个问题吗?
project.rb
class Project < ActiveRecord::Base
attr_accessible :name, :category_id, :user_id, :goal, :about, :headline, :image_url, :about_html, :blog_post_attributes
has_many :blog_posts, :dependent => :destroy
accepts_nested_attributes_for :blog_posts
end
blog_post.rb
class BlogPost < ActiveRecord::Base
attr_accessible :project_id, :user_id, :title, :blog_text, :commentable, :private
belongs_to :project
has_many :blog_post_replies, :dependent => :destroy
end
项目/ _new_form.html.erb
<%= semantic_form_for @project do |f| %>
<%= f.semantic_errors :state %>
<%= f.input :name, :as => :string %>
<%= f.input :headline, :as => :string %>
<%= f.input :about %>
<%= f.input :image_url, :as => :file %>
<%= f.input :category , :include_blank => false%>
<div class="well">
<%= f.semantic_fields_for :blog_post do |blog_post| %>
<%= blog_post.input :title, :as => :string, :label => "First Blog Post Title" %>
<%= blog_post.input :blog_text, :as => :text, :label => "First Blog Post Text" %>
<% end %>
</div>
<%= f.action :submit, :as => :button %>
<% end %>
编辑如果我使用建议的Formtastic格式:
<%= f.input :title, :for => :blog_post %>
<%= f.input :blog_text, :for => :blog_post %>
我收到此错误;这让我觉得它根本没有看到嵌套属性:
NoMethodError in Projects#new
undefined method `title' for #<Project:0x007fb044745f58>
答案 0 :(得分:0)
错误在于:
<%= f.semantic_fields_for :blog_post do |blog_post| %>
应该是:
<%= f.semantic_fields_for :blog_posts do |blog_post| %>
(:blog_posts而不是:blog_post)