简体   繁体   English

Rails 表单助手:多个嵌套模型,如一种形式的“Article.comments”?

[英]Rails form helper: Multiple nested models like "Article.comments" in one form?

Here is the form_with view-helper from the Rails "Getting Started" Guide ( https://guides.rubyonrails.org/getting_started.html ) for the nested Article model Comment or Article.comments :这是 Rails“入门”指南 ( https://guides.rubyonrails.org/getting_started.html ) 中嵌套的文章 model CommentArticle.commentsform_with view-helper:

<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 %>

Also

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

and

class Comment < ApplicationRecord
  belongs_to :article
end

Now I would like to know if it is possible to use the form_with helper or another helper or helper-combination in order to create or edit a new Article with more than one nested models like Comment, Tag, ... and what further models an Article may be composed of.现在我想知道是否可以使用form_with helper 或另一个 helper 或 helper-combination 来创建或编辑具有多个嵌套模型(如 Comment、 Tag等)的新文章文章可能由。

... which creates a sane and useful params-Hash (because my own solution with a 'fields_for' form-helper doesn't produce a desired or useful params hash. ...它创建了一个合理且有用的参数哈希(因为我自己的带有“fields_for”表单助手的解决方案不会产生所需或有用的参数 hash。

This is how the params-hash looks like:这是 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>

The problem is that the controller/article id is not subsumed under the :article key.问题是控制器/文章 id 没有包含在:article键下。 I don't know how to fix that for strong_parameters and I don't even want to.我不知道如何为 strong_parameters 解决这个问题,我什至不想这样做。 I would prefer Rails to just function after the principle of least astonishment instead of doing hackery things to get things working.根据最小惊讶原则,我更喜欢 Rails 而不是 function,而不是做一些骇人听闻的事情来让事情正常进行。

In this case I hope it's my own ignorance and lack of knowledge regarding form-helpers that prevents Rails from generating a proper params-hash.在这种情况下,我希望是我自己对 form-helper 的无知和缺乏知识导致 Rails 无法生成正确的 params-hash。

Thanks.谢谢。

Rails should be doing this as you expect. Rails 应该按照您的预期执行此操作。 From accepts_nested_attributes :来自accepts_nested_attributes

Consider a member that has a number of posts:考虑一个拥有多个帖子的成员:

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

You can now set or update attributes on the associated posts through an attribute hash for a member: include the key:posts_attributes with an array of hashes of post attributes as a value.您现在可以通过成员的属性 hash 设置或更新关联帖子的属性:包括 key:posts_attributes 以及帖子属性的散列数组作为值。

For each hash that does not have an id key a new record will be instantiated, unless the > hash also contains a _destroy key that evaluates to true.对于每个没有 id 键的 hash,将实例化一条新记录,除非 > hash 还包含一个评估为 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'

The params hash you are showing is very strange.您显示参数 hash 很奇怪。 It looks like you're manually making disconnected fields like parent_article_id instead of actually using the capaibilities of form_with and fields_for .看起来您是在手动创建断开连接的字段,例如parent_article_id而不是实际使用form_withfields_for的功能。

I'd need to see your view and controller code to see how you've implemented form_with and fields_for to help you get these params nested the way you want.我需要查看您的视图和 controller 代码,以查看您如何实现form_withfields_for以帮助您按照您想要的方式嵌套这些参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM