ruby-on-rails - simple_form 中的嵌套属性返回质量分配错误

标签 ruby-on-rails ruby nested-attributes simple-form mass-assignment

模型:

class Topic < ActiveRecord::Base
  has_many        :posts, :dependent => :destroy
  validates       :name, :presence => true,
                  :length => { :maximum => 32 }
  attr_accessible :name, :post_id
end

class Post < ActiveRecord::Base
  belongs_to :topic,    :touch => true
  has_many   :comments, :dependent => :destroy
  accepts_nested_attributes_for :topic
  attr_accessible :name, :title, :content, :topic, :topic_attributes
end

查看:

<%= simple_form_for :post, :url => { :controller => :posts, :action => "create" } do |f| %>
  <h1>Create a Post</h1>
  <%= f.input :name, :label => false, :placeholder => "Name" %>
  <%= f.input :title, :label => false, :placeholder => "Title" %>
  <%= f.input :content, :label => false, :placeholder => "Content", :as => :text %>
  <%= f.simple_fields_for :topic do |topic_form| %>
    <%= topic_form.input :name, :label => false, :placeholder => "Topic" %>
  <% end %>
  <%= f.button :submit, "Post" %>  
<% end %>

Controller :

def create
  topic_name = params[:post].delete(:topic)
  @topic = Topic.find_or_create_by_name(topic_name)
  @post = Post.new(params[:post])
  @post.topic = @topic
  respond_to do |format|
    if @post.save
      format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

当我填写并提交/posts/new 表单时,终端会显示:

Started POST "/posts" for 127.0.0.1 at 2011-05-06 18:30:10 -0700
  Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Gg2T18NRiYMe5UMReSfvwwiDTQSrjdnjiEdyDcP1MuI=", "post"=>{"name"=>"25", "title"=>"25", "content"=>"25", "topic"=>{"name"=>"25"}}, "commit"=>"Post"}
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics" WHERE "topics"."name" = '25' LIMIT 1
  AREL (0.9ms)  INSERT INTO "topics" ("name", "created_at", "updated_at") VALUES ('25', '2011-05-07 01:30:10.237406', '2011-05-07 01:30:10.237406')
  Topic Load (0.7ms)  SELECT "topics".* FROM "topics" ORDER BY updated_at DESC
  AREL (0.8ms)  INSERT INTO "posts" ("name", "title", "content", "topic_id", "created_at", "updated_at") VALUES ('25', '25', '25', 25, '2011-05-07 01:30:10.406437', '2011-05-07 01:30:10.406437')
  AREL (0.3ms)  UPDATE "topics" SET "updated_at" = '2011-05-07 01:30:10.415141' WHERE "topics"."id" = 25
Redirected to http://0.0.0.0:3000/posts/25
Completed 302 Found in 281ms

我尝试将 :post_attributes 添加到 Topic 模型中的 attr_accessible 中,但这并没有解决问题。可以是表格吗?

最佳答案

您不应该为 :topic 使用 simple_fields_for 吗?

simple_form_for @post do |f|
  f.simple_fields_for :topic do |topic_form|
    topic_form.input :name
  end
end

关于ruby-on-rails - simple_form 中的嵌套属性返回质量分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5892159/

相关文章:

ruby-on-rails - 在Rails3中覆盖到/(根)的资源路由: not changing the path helper?

javascript - 如何在 Rails 应用程序中从 Javascript 写入 Heroku 的日志?

ruby-on-rails - 将设计用于多个模型

ruby - Ruby 从 1.8.7 升级到 1.9.3 后出现 Yaml 编码问题

ruby-on-rails - 如何排序不区分大小写的关联列

ruby-on-rails - 有没有一种工具可以在编码时自动保存对文件的增量更改?

ruby - 访问存储在实例变量中的线程的线程变量

ExtJS 4 问题,尝试对深层嵌套的 JSON 数据进行更新

ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中

ruby-on-rails - Rails - 用一个链接更新多个资源条目