ruby-on-railsform-helpers

Rails: Why do I get such a strange params hash?


I have an Article model which has tags and following 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 %>

For which I get on update following example hash:

>> 

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>

Why is

"controller"=>"articles", "action"=>"update", "id"=>"1"

out of the "article" key? It clearly belongs to the article information.

Yours sincerely

von Spotz


Solution

  • Those 3 params comes from the router (you probably have a resources :articles entry there, if you inspect the routes rails routes you'll see the template of the routes has an :id segment).

    params[:article] comes from the from input fields: if you inspect the input elements you'll notice the name attribute is article[title], article[text], etc. Notice you only have an article hash there because of the inputs' names.

    Maybe you are thinking of that in a different way, but for me it makes sense that those are separeted from the article key, because they serve different purposes: first 3 serves the purpose of finding a controller, an action and an article, the article hash serves the purpose of grouping the user input.