Home > database >  Nested parameter does not include <name>_attributes in Rails 7 when submitting nested attribut
Nested parameter does not include <name>_attributes in Rails 7 when submitting nested attribut

Time:04-27

I'm facing this issue and after digging, I can't seem to find a solution for this.

I'm trying to create a user and his meet_infos on a nested form, but in the params, I always end up with "meet_infos" instead of "meet_infos_attributes":

Unpermitted parameters: :page_id, :meets_infos. Context: { controller: Users::MeetInfosController, action: create, request: #<ActionDispatch::Request:0x00007f224cc73128>, params: {"authenticity_token"=>"[FILTERED]", "user"=>{"page_id"=>"1", "last_name"=>"nxgfbngh", "first_name"=>"n hgn cgn", "phone"=>"nfcntrsbgxd", "email"=>"hbfdhnbytdgbn", "meets_infos"=>{"page_id"=>"1", "reason"=>"dfgnhgdn", "datetime(1i)"=>"2022", "datetime(2i)"=>"4", "datetime(3i)"=>"26", "datetime(4i)"=>"11", "datetime(5i)"=>"17"}}, "commit"=>"Save User", "controller"=>"users/meet_infos", "action"=>"create", "page_id"=>"1"} }

My form:

<%= simple_form_for :user do |f| %>
 <%= f.error_notification %>

  <%= f.input :page_id, as: :hidden, input_html: { value: @page.id } %>

  <%= f.input :last_name %>
  <%= f.input :first_name %>
  <%= f.input :phone, :as => :tel %>
  <%= f.input :email, :as => :email %>

  <%= f.simple_fields_for :meet_infos do |mf| %>
    <%= mf.input :page_id, as: :hidden, input_html: { value: @page.id } %>
    <%= mf.input :reason, :as => :text %>
    <%= mf.input :datetime, :as => :datetime %>
  <% end %>


  <%= f.submit %>
<% end %>

My controller:

class Users::MeetInfosController < ApplicationController
  skip_before_action :authenticate_admin!
  before_action :set_page, only: %i[index create destroy]

  def index; end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to page_users_meet_infos_path
      flash[:success] = 'Rdv pris'
    else
      render :index
    end
  end

  private

  def user_params
    params.require(:user).permit(:id, :last_name, :first_name, :phone, :email, { meet_infos_attributes: [:id, :reason, :datetime, :_destroy] }).merge(page_id: @page.id)
  end

  def set_page
    @page = Page.find(params[:page_id])
  end
end

User model:

class User < ApplicationRecord
  belongs_to :page
  has_many :meet_infos, dependent: :destroy
  accepts_nested_attributes_for :meet_infos

  validates_presence_of :page_id
  validates :last_name, presence: true
  validates :first_name, presence: true
  validates :email, presence: true, uniqueness: true
  validates :phone, presence: true, uniqueness: true
end

MeetInfo model:

class MeetInfo < ApplicationRecord
  belongs_to :page
  belongs_to :user

  validates_presence_of :user
  validates_presence_of :page_id
  validates :reason, presence: true, uniqueness: true
  validates :datetime, presence: true, uniqueness: true
end

CodePudding user response:

You have a sneaky nil error. Use:

<%= simple_form_for @user do |f| %>

Calling form_for and simple_form_for with a symbol and having it resolve an instance variable was a stupid legacy feature that has been depreachiated for a long time and I guess the time to axe it finally came.

Take this simple example:

<%= simple_form_for(@country) do |form| %>
  <% debugger form.object %>
  <%= form.input :name %>
  <fieldset>
    <legend>Cities</legend>
    <%= form.simple_fields_for :cities do |cities| %>
        <%= cities.input :name %>
    <% end %>
  </fieldset>
<% end %>

This will yield the model that you have passed to simple_form_for to the debugger. If you change it to a symbol simple_form_for(:country) you'll get nil instead and the symbol seems to just be used as the scope argument for form_with - and since there is no model fields_for is just using the symbol :meet_infos instead of calling the method on the object.

It could be argued that simple_form_for instead should have raised an error and warned that the behavior is deprechiated.

  • Related