简体   繁体   English

Ruby on rails-简单形式,不会显示单选的错误。 其他错误显示正常

[英]Ruby on rails - Simple form not displaying errors for radio select. Other errors display fine

I have a user registration form (Devise) with 4 elements; 我有一个包含4个元素的用户注册表格(Devise); Username, email, password and 'user type'. 用户名,电子邮件,密码和“用户类型”。 User type is a boolean and is displayed as a radio select on the form. 用户类型为布尔值,并在表单上显示为单选。

Errors for Username, email and password show no problem, but I get no errors showing if a user doesn't select one of the radio buttons. 用户名,电子邮件和密码的错误没有问题,但是如果用户未选择任何一个单选按钮,则不会显示任何错误。 Validation IS in place, and the form won't send without one of the radio buttons selected, but I get no errors. 验证已就位,并且没有选择任何一个单选按钮就不会发送表格,但是我没有收到任何错误。

The form: 表格:

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
            <%= f.error_notification %>
        <div class="role-selector">
            <p class="type-sel-txt"><%= t('front.whattype') %></p>
            <label for="artistuser">
                <%= f.radio_button :artist, "1", :id => "artistuser" %>
                <span class="artistuser-sel"><%= t('front.bandm') %></span>
            </label>
            <label for="eventuser">
                <%= f.radio_button :artist, "0", :id => "eventuser" %>
                <span class="eventuser-sel"> <%= t('front.evento') %></span>
            </label>
        </div>
        <%= f.input :username, required: true, autofocus: true, placeholder: t('forms.login.user'), label: false %>
        <%= f.input :email, required: true, placeholder: t('forms.login.email'), label: false %>
        <%= f.input :password, required: true, placeholder: t('forms.login.password'), label: false %>
        <div id="passcheck"></div>
        <%= f.button :submit, t("forms.login.signup"), id: "register-btn" %>
    <% end %>

User.rb: User.rb:

  validates_inclusion_of :artist, in: [true, false], on: :create

Registration works without a problem, my only issue is the error not showing. 注册没有问题,我唯一的问题是错误未显示。 I'm not sure if it is necessary to paste any more of my code, but if so I'll update with whatever is required. 我不确定是否需要粘贴更多代码,但是如果是这样,我将使用所需的内容进行更新。

As opposed to simple form's f.input which handles all related tags besides the input itself (the label tag, the error message tag), f.radio_button handles only the input field, because it is actually a helper from ActionView , not simple form. 与简单形式的f.input处理输入本身之外的所有相关标签(标签标签,错误消息标签) f.radio_buttonf.radio_button仅处理输入字段,因为它实际上ActionView的帮助者 ,而不是简单形式。

Simple form generates a span with the "error" class by default when a field has some errors attached. 当字段附加了一些错误时,默认情况下,简单表单会使用“错误”类生成一个span I guess you will have to render the span yourself, in a similar way as you did for the field labels: 我想您将不得不像使用字段标签那样,自行绘制跨度:

<div class="role-selector">
  <p class="type-sel-txt"><%= t('front.whattype') %></p>
  <label for="artistuser">
     <%= f.radio_button :artist, "1", :id => "artistuser" %>
     <span class="artistuser-sel"><%= t('front.bandm') %></span>
  </label>
  <label for="eventuser">
     <%= f.radio_button :artist, "0", :id => "eventuser" %>
     <span class="eventuser-sel"> <%= t('front.evento') %></span>
  </label>

  <!-- show error for the artist attribute, if present -->
  <% if resource.errors['artist'].present? %>
    <span class="error"><%= resource.errors['artist'] %></span>
  <% end %>
</div>

I can't figure it whole out what's happening but you can try some hack kinda thing 我无法弄清楚发生了什么,但是您可以尝试一些技巧

<%= f.radio_button :artist, "1", :id => "artistuser", :selected => f.object.artist %>

and then you can check whether it is present or not 然后您可以检查它是否存在

<% if f.object.artist.blank? %>
  Display Your Error Message

Just a rough idea but with little modification you can make it work, let me know if there's any problem. 只是一个粗略的想法,但只需进行少量修改即可使其生效,如果有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM