隐藏字段无法正确提交 - Rails

时间:2015-01-04 00:14:45

标签: ruby-on-rails forms hidden-field

我试图在rails中提交一个隐藏字段,将我的布尔字段'internal_verifier'设置为false。由于某些原因,这不起作用。

     <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>

这是我的表单,我遇到了隐藏字段'internal_verifier'的问题,尽管在终端中这是提交的示例输入。

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"}

出于某种原因,我仍然在提交时收到此错误消息。

Users internal verifier can't be blank

控制器操作:

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

强参数

 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

我正在对内部验证程序的存在进行验证,这是我为它运行的唯一验证。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

此时,发布的所有代码都是正确的。检查文档后,我发现布尔值无法验证是否存在。因为false.blank? => true

检查布尔值的方法是:validates_inclusion_of :field_name, in: [true, false],您可以阅读官方文档here