Rails: 为什么我得到这么奇怪的参数散列?
Rails: Why do I get such a strange params hash?
我有一个 Article
模型,它有 tags
和以下 edit.html.erb
/app/models/article.rb
class Article < ApplicationRecord
include Taggable
has_many :child_articles, class_name: "Article",
foreign_key: "parent_article_id"
belongs_to :parent_article, class_name: "Article", optional: true
accepts_nested_attributes_for :tags
end
/app/views/articles/edit_form.html.erb
<title>_edit_form.html.erb</title>
_edit_form.html.erb
<%= form_with(model: @article, local: true, method: :patch, url: article_path) do |article| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= article.hidden_field :parent_article_id, value: @article.parent_article_id %>
<p>
<%= article.label :title %><br>
<%= article.text_field :title, value: "#{@article.title}" %>
</p>
<p>
<%= article.label :text %><br>
<%= article.text_area :text, value: "#{@article.text}", size: 200 %>
</p>
<p>
<div style="font-weight: bold;">tags</div>
<%= article.fields_for :tags do |article_tags| %>
<%= article_tags.text_field :name, value: "#{@article.tags.map(&:name).join(", ")}" %>
<% end %>
</div>
</p>
<p>
<%= article.submit %>
</p>
<% end %>
为此,我开始更新以下示例哈希:
>>
params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"NK/5QEf/723CfgznpMCWT0/itHUj68gz5GmzD+nDzaLM6NILLex7/Co809QILxRUgegCwOppr/S8PN8vK3Avfw==", "article"=>{"parent_article_id"=>"", "title"=>"Überschrift", "text"=>"Toller Text", "tags_attributes"=>{"0"=>{"name"=>"Rails, Ruby"}}}, "commit"=>"Update Article", "controller"=>"articles", "action"=>"update", "id"=>"1"} permitted: false>
为什么是
"controller"=>"articles", "action"=>"update", "id"=>"1"
出"article"
键?明明属于文章信息
此致
冯·斯波茨
这 3 个参数来自路由器(你可能在那里有一个 resources :articles
条目,如果你检查路由 rails routes
你会看到路由模板有一个 :id
段)。
params[:article]
来自输入字段:如果您检查输入元素,您会注意到 name
属性是 article[title]
、article[text]
等。注意由于输入的名称,您只有一个 article
哈希。
也许您正在以不同的方式考虑它,但对我来说,将它们与 article
键分开是有道理的,因为它们有不同的用途:前 3 个用于查找控制器、一个动作和一篇文章,article
散列用于对用户输入进行分组。
我有一个 Article
模型,它有 tags
和以下 edit.html.erb
/app/models/article.rb
class Article < ApplicationRecord
include Taggable
has_many :child_articles, class_name: "Article",
foreign_key: "parent_article_id"
belongs_to :parent_article, class_name: "Article", optional: true
accepts_nested_attributes_for :tags
end
/app/views/articles/edit_form.html.erb
<title>_edit_form.html.erb</title>
_edit_form.html.erb
<%= form_with(model: @article, local: true, method: :patch, url: article_path) do |article| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= article.hidden_field :parent_article_id, value: @article.parent_article_id %>
<p>
<%= article.label :title %><br>
<%= article.text_field :title, value: "#{@article.title}" %>
</p>
<p>
<%= article.label :text %><br>
<%= article.text_area :text, value: "#{@article.text}", size: 200 %>
</p>
<p>
<div style="font-weight: bold;">tags</div>
<%= article.fields_for :tags do |article_tags| %>
<%= article_tags.text_field :name, value: "#{@article.tags.map(&:name).join(", ")}" %>
<% end %>
</div>
</p>
<p>
<%= article.submit %>
</p>
<% end %>
为此,我开始更新以下示例哈希:
>>
params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"NK/5QEf/723CfgznpMCWT0/itHUj68gz5GmzD+nDzaLM6NILLex7/Co809QILxRUgegCwOppr/S8PN8vK3Avfw==", "article"=>{"parent_article_id"=>"", "title"=>"Überschrift", "text"=>"Toller Text", "tags_attributes"=>{"0"=>{"name"=>"Rails, Ruby"}}}, "commit"=>"Update Article", "controller"=>"articles", "action"=>"update", "id"=>"1"} permitted: false>
为什么是
"controller"=>"articles", "action"=>"update", "id"=>"1"
出"article"
键?明明属于文章信息
此致
冯·斯波茨
这 3 个参数来自路由器(你可能在那里有一个 resources :articles
条目,如果你检查路由 rails routes
你会看到路由模板有一个 :id
段)。
params[:article]
来自输入字段:如果您检查输入元素,您会注意到 name
属性是 article[title]
、article[text]
等。注意由于输入的名称,您只有一个 article
哈希。
也许您正在以不同的方式考虑它,但对我来说,将它们与 article
键分开是有道理的,因为它们有不同的用途:前 3 个用于查找控制器、一个动作和一篇文章,article
散列用于对用户输入进行分组。