【问题标题】:Nested forms in Rails using has_many :throughRails 中的嵌套表单使用 has_many :through
【发布时间】:2011-10-22 23:01:51
【问题描述】:

我无法弄清楚如何使用 has_many :through 关系制作嵌套表单。我使用了这个Railscast 并查看了this tutorial 以及有关 Stack Overflow 和 Google 其他地方的许多问题。

我正在尝试通过文章表单创建标签。我的代码已经根据来自许多不同来源的信息进行了多次迭代,但没有一个有效,但现在我有

文章类

class Article < ActiveRecord::Base
    attr_accessible :content, :heading, :image, :tag_ids, :tags, :tag_name, :tag_attributes

  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :article_tags
  has_many :tags, :through => :article_tags
  accepts_nested_attributes_for :tags, :reject_if => proc { |attributes| attributes['tag_name'].blank? }
...
end

标签类

class Tag < ActiveRecord::Base
  attr_accessible :tag_name

  has_many :article_tags
  has_many :articles, :through => :article_tags
end

article_tags 的类

class ArticleTag < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag
end

我的articles_controller.rb 中的New 是这样的:

def new
  @article = Article.new
  @tags = Tag.find(:all)
  article_tag = @article.article_tags.build()
  @article_tags = @article.tags.all
  @article.article_tags.build.build_tag
  3.times do
      article_tag = @article.article_tags.build()
  end
end

而我的文章表单目前是这样的(我在将 fields_for :tags 嵌套在 fields_for :article_tags 内或让它们自己独立之间来回走动):

<%= form_for @article , :html => { :multipart => true } do |f| %>
 ...excerpted...
<%= f.fields_for :article_tags do |t| %>
  <%= t.fields_for :tags do |ta| %>
    <%= ta.label :tag_name, "Tag name" %>
    <%= ta.text_field :tag_name %>
  <% end %>
<% end %>

我意识到这可能很混乱;我对此很陌生,我正在努力弄清楚。我必须在articles_controller 创建中添加任何内容吗?它与 attr_accessible 有关吗?还是我应该做一些完全不同的事情?

编辑:

以下是在进行 Hck 建议的更改并创建新文章、选择 tag_id 为 3 的现有标签并尝试同时创建新标签后的请求参数:

在 2011-08-10 19:05:46 +1000 为 127.0.0.1 开始 POST "/articles" 由 ArticlesController#create 作为 HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"测试标题", "content"=>"测试内容", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"测试标签"}}}, "commit"=>"提交"} 用户负载 (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 警告:无法批量分配受保护的属性:article_tags 标签加载 (0.4ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1 AREL (0.4ms) INSERT INTO "articles" ("content", "user_id", "created_at", "updated_at", "heading", "image_file_name", "image_content_type", "image_file_size") VALUES ('Test Content', 1, '2011-08-10 09:05:46.228951', '2011-08-10 09:05:46.228951', '测试标题', NULL, NULL, NULL) AREL (0.2ms) INSERT INTO "article_tags" ("article_id", "tag_id", "created_at", "updated_at") VALUES (88, 3, '2011-08-10 09:05:46.243076', '2011-08 -10 09:05:46.243076') [回形针] 保存附件。 重定向到 [本地主机] 完成 302 发现在 212 毫秒内

如果我将 :article_tags 添加到文章的 attr_accessible 并重试,我会得到:

在 2011-08-10 19:11:49 +1000 为 127.0.0.1 开始 POST "/articles" 由 ArticlesController#create 作为 HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"测试标题", "content"=>"测试内容", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"测试标签"}}}, "commit"=>"提交"} 用户负载 (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 标签加载 (0.4ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1 在 119 毫秒内完成

ActiveRecord::AssociationTypeMismatch (ArticleTag(#2165285820) 预期,得到 Array(#2151973780)): app/controllers/articles_controller.rb:32:in `create'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 nested-forms has-many-through


    【解决方案1】:

    我认为您也不必将文章标签嵌套在其中。文章标签只是文章和标签之间的关联。您可以简单地在文章中创建新标签,因为您已经与它们相关联。我相信它来自“accepts_nested_attributes”的魔力。试试这个。

    <%= form_for @article , :html => { :multipart => true } do |f| %>
     ...excerpted...
      <%= f.fields_for :tags, Tag.new do |t| %>
        <%= t.label :tag_name, "Tag name" %>
        <%= t.text_field :name %>
      <% end %>
    <% end %>
    

    此外,您应该尝试批量分配它,而不是使用私有参数逐个保存每个属性。我之前也遇到过嵌套表单问题,所以你可以看看我是如何编写代码的: Cannot save record to database RAILS nested forms

    我唯一遗漏的是私人参数部分,我建议你这样做。

    private
        def venue_params
          params.require(:venue).permit(:name, :address, :discount, :latitude, :longitude, :tags_attributes =>[:name],:tag_ids => [])
        end
    

    我还写了一篇关于嵌套表单的博文,你也可以看看

    http://minling.github.io/

    【讨论】:

      【解决方案2】:

      尝试在控制器的操作中将@article.article_tags.build.build_tag 替换为@article.tags.build

      【讨论】:

      • 谢谢,但还是不行。它创建没有错误的新文章,但未创建或分配新标签。
      • 请在提交表单后发布请求参数。
      • 我编辑了我的问题;如果你不是你要找的,请告诉我。感谢您的观看。
      猜你喜欢
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2012-03-10
      • 2011-10-07
      • 1970-01-01
      相关资源
      最近更新 更多