Search code examples
ruby-on-railsformshidden-field

Hidden Field not submitting properly - Rails


I am trying to submit a hidden field in rails to set my boolean field 'internal_verifier' to false. For some reason this isn't working.

     <div class="wrapper">

      <%= render 'shared/errors', { model: @client } %>

      <h2>Client Information</h2>

      <%= form_for @client, :url => url_for(:controller => 'clients', :action => 'initial_client_create') do |f| %>

    <div class="field-row">
      <%= f.label :full_name, class: 'form-icon' %>
      <%= f.text_field :full_name, :placeholder => 'Full Name' %>
    </div>

    <div class="field-row">
      <%= f.label :short_name, class: 'form-icon' %>
      <%= f.text_field :short_name, :placeholder => 'Short Name' %>
    </div>

    <div class="field-row">
      <%= f.label :address1, "Address 1", class: 'form-icon' %>
      <%= f.text_field :address_1, :placeholder => "Address" %>
    </div>

     <div class="field-row">
      <%= f.label :city, class: 'form-icon' %>
      <%= f.text_field :city, :placeholder => "City" %>
    </div>

     <div class="field-row">
      <%= f.label :country, class: 'form-icon' %>
      <%= f.text_field :country, :placeholder => "Country" %>
    </div>

     <div class="field-row">
      <%= f.label :postcode, class: 'form-icon' %>
      <%= f.text_field :postcode, :placeholder => "Postcode" %>
    </div>

     <div class="field-row">
      <%= f.label :industry, class: 'form-icon' %>
      <%= f.text_field :industry, :placeholder => "Industry" %>
    </div>

    <h2>User Information</h2>

    <%= f.fields_for :users do |uf| %>

    <div class="field-row">
      <%= uf.label :first_name, class: 'form-icon' %>
      <%= uf.text_field :first_name, :placeholder => 'First Name' %>
    </div>

    <div class="field-row">
      <%= uf.label :last_name, class: 'form-icon' %>
      <%= uf.text_field :last_name, :placeholder => 'Last Name' %>
    </div>

    <div class="field-row">
      <%= uf.label :email, class: 'form-icon' %>
      <%= uf.email_field :email, :placeholder => "Email" %>
    </div>

    <div class="field-row">
      <%= uf.label :password %>
      <%= uf.password_field :password, :placeholder => "Password" %>
    </div>

     <div class="field-row">
      <%= uf.label :password_confirmation %>
      <%= uf.password_field :password_confirmation, :placeholder => "Password Confirmation" %>
    </div>

    <%= uf.hidden_field :role, value: 'standard_admin' %>
    <%= uf.hidden_field :internal_verifier, value: false %>

    <% end %>

    <%= f.submit "Register", class: 'button' %>
  <% end %>

  <%#= render "devise/shared/links" %>
</div>

This is my form, I am having trouble with the hidden field 'internal_verifier' although in the terminal this is the example inputs which are being submitted.

Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ug/AqhtPbUN+JnwAg+9tBpcLrOb+W8Rqmfbf+ifax50=", "client"=>{"full_name"=>"Miro Yolov Co", "short_name"=>"Miro Co", "address_1"=>"Address Miro", "city"=>"Miro City", "country"=>"Miro Country", "postcode"=>"Postcode", "industry"=>"Industry", "users_attributes"=>{"0"=>{"first_name"=>"Miro", "last_name"=>"Yolov", "email"=>"miroyolov@hotmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"standard_admin", "internal_verifier"=>"false"}}}, "commit"=>"Register"}

For some reason I still get this error message on submission.

Users internal verifier can't be blank

Controller Actions:

def initial_client_create

    @client = Client.new(initial_client_params)

    if @client.save

      redirect_to @client, notice: 'Client was successfully created.'

    else
      render 'devise/registrations/new', notice: 'Failure to create'
    end
end


 def initial_create

    @user = @client.users.new(user_registration_params)

    if @user.save

    else
      render action: 'new'
    end

  end

Strong Parameters

 def initial_client_params
   params.require(:client).permit(:full_name, :short_name, :address_1, :city, :country, :postcode,   :telephone, :industry, users_attributes: [:first_name, :last_name, :email, :password, :password_confirmation, :role, :client_id, :internal_verifier] )
 end

  def user_registration_params

    params.require(:user).permit(:internal_verifier, :client_id, :first_name, :last_name, :email, :password, :password_confirmation, :role)

  end

I am running a validation on the presence of internal verifier, this is the only validations I am running for it. Any help would be appreciated. Thank you.


Solution

  • At this point, all the code published is wright. After check the documentation, I find that a boolean value can't be validated for presence. Because false.blank? => true.

    The way of check a boolean value is: validates_inclusion_of :field_name, in: [true, false], you can read the oficial documentation here.