【问题标题】:Rails form helper: Multiple nested models like "Article.comments" in one form?Rails 表单助手:一个表单中的多个嵌套模型,如“Article.comments”?
【发布时间】:2020-10-26 14:03:04
【问题描述】:

这里是 Rails“入门”指南 (https://guides.rubyonrails.org/getting_started.html) 中的 form_with 视图助手,用于嵌套文章模型 CommentArticle.comments

<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>
 
<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>
 
<h2>Add a comment:</h2>
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>
 
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

还有

class Article < ApplicationRecord
  has_many :comments
  validates :title, presence: true,
                    length: { minimum: 5 }
end

class Comment < ApplicationRecord
  belongs_to :article
end

现在我想知道是否可以使用 form_with 助手或其他助手或助手组合来创建或编辑具有多个嵌套模型的新文章,例如评论、标签、 ...以及文章可以由哪些进一步的模型组成。

...它创建了一个理智且有用的参数哈希(因为我自己的带有“fields_for”表单助手的解决方案不会产生所需或有用的参数哈希。

这就是 params-hash 的样子:

<ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"BeCtYS/U6lugXzzplTEBsMXAiD0x7z28iBUblHiza379p4YqRcd+ykgd49o53oOrC8o+iPhtWnvQQHe0ugCJow==", "article"=>{"parent_article_id"=>"", "title"=>"Überschrift", "text"=>"Toller Text"}, "tags"=>{"name"=>"Rails"}, "commit"=>"Update Article", "controller"=>"articles", "action"=>"update", "id"=>"1"} permitted: false>

问题在于控制器/文章 ID 未包含在 :article 键下。我不知道如何为 strong_parameters 解决这个问题,我什至不想这样做。我更希望 Rails 只是按照最小惊讶原则运行,而不是做一些骇人听闻的事情来让事情正常运行。

在这种情况下,我希望是我自己对表单助手的无知和缺乏知识导致 Rails 无法生成正确的 params-hash。

谢谢。

【问题讨论】:

  • 我想你在找accepts_nested_attributes_for,也就是here
  • 谢谢,但已经添加到我的模型中
  • accepts_nested_attributes 确实在父母的“孩子”模型参数中嵌套,所以你应该得到你所要求的。您可以发布自己的视图和控制器代码吗?你的参数看起来很奇怪,我猜你没有正确实现表单。

标签: ruby-on-rails strong-parameters view-helpers


【解决方案1】:

Rails 应该按照您的预期执行此操作。 来自accepts_nested_attributes

考虑一个有很多帖子的成员:

class Member < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts
end

您现在可以通过成员的属性散列设置或更新关联帖子的属性:将键 :posts_attributes 包含在帖子属性散列数组中作为值。

对于每个没有 id 键的散列,将实例化一条新记录,除非 > 散列还包含评估为 true 的 _destroy 键。

params = { member: {
  name: 'joe', posts_attributes: [
    { title: 'Kari, the awesome Ruby documentation browser!' },
    { title: 'The egalitarian assumption of the modern citizen' },
    { title: '', _destroy: '1' } # this will be ignored
  ]
}}


member = Member.create(params[:member])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'

您显示的参数哈希 非常奇怪。看起来您正在手动创建像 parent_article_id 这样的断开连接的字段,而不是实际使用 form_withfields_for 的功能。

我需要查看 您的 视图和控制器代码,以了解您如何实现 form_withfields_for 以帮助您以所需的方式嵌套这些参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-19
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多