我看过其他帖子,看不出我做错了什么。我收到此错误:
ActiveModel::MassAssignmentSecurity::Error in ProjectsController#create
Can't mass-assign protected attributes: blog_post
这是我的两个模型和我的表格。我的印象是项目控制器中没有什么特别需要的。项目或 blog_post 控制器中有什么东西可以搞砸吗?
项目.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>