brpl20
brpl20

Reputation: 13

Rails params on simpleform exist but not going through ActionController::ParameterMissing strong params

On create I'm getting missing paramters.

Rails Error: `ActionController::ParameterMissing in ClientsController#create`

Console params.inspect:

<ActionController::Parameters {\"utf8\"=>\"✓\", \"authenticity_token\"=>\"yJ/iKj06W/385SQ8aGUJAiDd94KiYgg3nxKueXxqr2FKcPy/hUb84GzUar3ILAumBc9PrCdKthQnvhf73UVrAw==\", \"client\"=>{\"genero\"=>\"0\", \"nome\"=>\"asdfasdf\", \"sobrenome\"=>\"asdf \", \"nacionalidade\"=>\"\", \"estadocivil\"=>\"asdf \", \"capacidade\"=>\"\", \"profissao\"=>\"\", \"empresa_atual\"=>\"\", \"nascimento(1i)\"=>\"2020\", \"nascimento(2i)\"=>\"3\", \"nascimento(3i)\"=>\"21\", \"nome_mae\"=>\"\", \"nb\"=>\"\", \"rg\"=>\"\", \"cpf\"=>\"\", \"email\"=>\"\", \"endereco\"=>\"\", \"cidade\"=>\"\", \"estado\"=>\"\", \"dados_bancarios\"=>\"\", \"cep\"=>\"\", \"telefone\"=>\"\", \"notas\"=>\"\"}, \"commit\"=>\"Create Client\", \"controller\"=>\"clients\", \"action\"=>\"create\"} permitted: false>

Rails S Console:

Completed 400 Bad Request in 4ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: nome):

app/controllers/clients_controller.rb:39:in `client_params'
app/controllers/clients_controller.rb:16:in `create'
Started PUT "/__web_console/repl_sessions/958f11109b1c4a183934f5e50e0c76b8" for ::1 at 2020-03-21 19:41:33 -0300
Started PUT "/__web_console/repl_sessions/958f11109b1c4a183934f5e50e0c76b8" for ::1 at 2020-03-21 19:41:38 -0300
Started PUT "/__web_console/repl_sessions/958f11109b1c4a183934f5e50e0c76b8" for ::1 at 2020-03-21 19:41:47 -0300

My controller is:

class ClientsController < ApplicationController


  def create
    @client = Client.new(client_params)
    if @client.save
      redirect_to clients_path
    else
      render :new
    end
  end

  private

  def client_params
    params.require(:nome).permit(:sobrenome, :nacionalidade)
  end

end

My model is:

class Client < ApplicationRecord
  attr_accessor :nome, :sobrenome, :nacionalidade, :asd, :nome, :genero, :nome, :sobrenome, :nacionalidade, :estadocivil, :capacidade, :profissao, :empresa_atual, :nascimento, :nome_mae, :nb, :rg, :cpf, :email, :endereco, :cidade, :estado, :dados_bancarios, :cep, :telefone, :notas
  validates :nome, presence: true
end

My routes:

Rails.application.routes.draw do
  get 'pages/home'
  root to: 'pages#home'
  resources :clients
end

SimpleForm views/clients/_form.html.erb and new.html.erb:

<%= simple_form_for @client do |f| %>
  <%= f.input :genero, label: 'Gênero' %>
  <%= f.input :nome, label: 'Nome' %>
  <%= f.input :sobrenome, label: 'Sobrenome' %>
  <%= f.input :nacionalidade, label: 'Nacionalidade' %>
  <%= f.input :estadocivil, label: 'Estado Civil' %>
  <%= f.input :capacidade, label: 'Capacidade' %>
  <%= f.input :profissao, label: 'Profissão' %>
  <%= f.input :empresa_atual, label: 'Empresa Atual' %>
  <%= f.input :nascimento, label: 'Data de Nascimento' %>
  <%= f.input :nome_mae, label: 'Nome da Mãe' %>
  <%= f.input :nb, label: 'Número de Benefício' %>
  <%= f.input :rg, label: 'Número de RG' %>
  <%= f.input :cpf, label: 'CPF' %>
  <%= f.input :email, label: 'E-mail' %>
  <%= f.input :endereco, label: 'Endereço' %>
  <%= f.input :cidade, label: 'Cidade' %>
  <%= f.input :estado, label: 'Estado' %>
  <%= f.input :dados_bancarios, label: 'Dados Bancários' %>
  <%= f.input :cep, label: 'CEP' %>
  <%= f.input :telefone, label: 'Telefone' %>
  <%= f.input :notas, label: 'Notas' %>
  <%= f.button :submit %>
<% end %>

<%= render 'form', client: @client %>

Even when I change strong params or anything, the params do not go, all the data is nil.

Upvotes: 1

Views: 129

Answers (1)

Yury Matusevich
Yury Matusevich

Reputation: 998

The issue is in method client_params

Change client_params to be:

def client_params
  params.require(:client).permit(:sobrenome, :nacionalidade)
end

params.require(:nome) expects that params hash has key nome that wraps other params you want to permit. Like this:

{ nome: { sobrenome: "", nacionalidade: "" }}. That's not your case based on your form.

When you add simple_form_for @client, rails builds html input tags with attribute name this way:

<input type="text" name="client[genero]">`
<input type="text" name="client[sobrenome]">`.
<!-- etc -->

So in your controller to permit genero, sobrenome you need to require client but not nome.

Upvotes: 1

Related Questions