简单的表单错误通知 [英] Simple Form Error Notification

查看:133
本文介绍了简单的表单错误通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出如何使用简单的表单与Rails,Devise,Omniauth和Bootstrap。我有几个问题,但其中一个是错误通知。



我有一个设计登录,并在Bootstrap模式中注册表单。注册表如下。请注意,我从表单中删除了所需的:true 函数,因为我不喜欢将星号附加到标签上(但是我确实尝试将其放回去,看看是否错误通知将会起作用。)

 <%if user_signed_in? %GT; 
< li>
<%= link_to('编辑注册',edit_user_registration_path)%>
< / li>
<%else%>
< li>
<%= link_to'Register',new_user_registration_path,:remote => true,'data-toggle'=> modal,data-target=> #myRModal,:method => 'get'%>
< / li>

< div class =modal fadeid =myRModaltabindex = - 1role =dialogaria-labelledby =myRModalLabelaria-hidden =true>
< div class =modal-dialog>
< div class =modal-content>
< div class =modal-header>
< button type =buttonclass =closedata-dismiss =modalaria-hidden =true>& times;< / button>
< h4 class =modal-titleid =myModalLabelstyle =color:black> Register< / h4>
< / div>

< div class =modal-body>
<% - 如果devise_mapping.omniauthable? %GT;
< div class =facebookauth> <%= link_to注册Facebook,user_omniauth_authorize_path(:facebook)%>< / div>
< br />
<%end - %>
<% - 如果devise_mapping.omniauthable? %GT;
< div class =linkedinauth> <%= link_to注册LinkedIn,user_omniauth_authorize_path(:linkedin)%>< / div>
< br />
<%end - %>
< / div>

< div class =modal-footer>



<%= simple_form_for(resource,as:resource_name,url:registration_path(resource_name))do | f | %GT;
<%= f.error_notification%>

< div class =form-inputsstyle =padding-left:20%; text-align:left; color:black;>

<%= f.input:first_name,占位符:'输入您的名字',autofocus:true,:input_html => {:maxlength => 15,:size => 40}%>
<%= f.input:last_name,占位符:'输入你的姓',autofocus:true,:input_html => {:maxlength => 15,:size => 40}%>
<%= f.input:email,占位符:'输入电子邮件',autofocus:true,:input_html => {:maxlength => 25,:size => 40}%>
<%= f.input:password,占位符:'至少8个字符',:input_html => {:maxlength => 15,:size => 40}%>
<%= f.input:password_confirmation,占位符:'确认您的密码',:input_html => {:maxlength => 15,:size => 40}%>
< / div>

< div class =form-actionsstyle =padding-left:20%; padding-top:5%; text-align:left; color:black;>
<%= f.button:submit,注册%><%end%>

我有一个用户模态,其中存在电子邮件地址和密码的验证。



我有一个用户/ registration_controller,具有以下创建功能:

  def create 
@user = User.new(user_params)#(params [:user])

respond_to do | format |
如果@ user.save
#在保存
之后,告诉UserMailer发送欢迎电子邮件#AdminMailer.new_user_waiting_for_approval.deliver

format.html {redirect_to(root_path,通知:'注册接收')}
format.json {render json:root_path,status::created,location:@user}
else
format.html {redirect_to(root_path,alert :对不起,你的注册有问题,请联系我们整理出来)
#format.html {render action:'new'}
format.json {render json: @ user.errors,status::unprocessable_entity}
end
end
end

我的应用程序助手和设计助手中都有设计助手,如下所示:

  def resource_name 
:用户
end

def资源
@resource || = User.new
end

def devise_mapping
@devise_mapping | | = Devise.mappings [:user]
结束

我的初始化文件有两个简单的表单文件。一个是simple_form.rb。另一个是simple_form_bootstrap.rb



表单中没有错误显示内嵌。当我填写没有电子邮件地址的表单时,我会收到我的registration_controller的create函数中出现的错误。



我真的希望出现错误,以表单形式,当用户点击提交时。



有谁知道如何解决这个问题?



谢谢。

解决方案

确保你有类似的行ur初始化器的包装器



b.use:error,wrap_with:{tag:'span',class:'help-block'}



如您在ur问题中提到的,请删除所需:false
和要删除所需的'*',请在config / locale / simple_form.en.yml中更改

 必需:
文本:'required'
标记:''


I am trying to figure out how to use Simple Form with Rails, Devise, Omniauth and Bootstrap. I have several problems, but one of them is error notifications.

I have a devise sign in and sign up form inside Bootstrap modals. The registration form is as follows. Note that I removed the required: true function from the form because I don't like the asterisk being appended to the label (but I did try putting it back to see if the error notifications would work).

<% if user_signed_in? %>
  <li>
    <%= link_to('Edit registration', edit_user_registration_path) %>
  </li>
<% else %>
  <li>
    <%= link_to 'Register', new_user_registration_path, :remote => true, 'data-toggle' => "modal", 'data-target' => "#myRModal", :method => 'get' %>
  </li>

  <div class="modal fade" id="myRModal" tabindex="-1" role="dialog" aria-labelledby="myRModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title" id="myModalLabel" style="color:black">Register</h4>
        </div>

        <div class="modal-body">
          <%- if devise_mapping.omniauthable? %>
            <div class="facebookauth"> <%= link_to "Register with Facebook", user_omniauth_authorize_path(:facebook) %></div>
            <br />
          <% end -%>
          <%- if devise_mapping.omniauthable? %>
            <div class="linkedinauth"> <%= link_to "Register with LinkedIn", user_omniauth_authorize_path(:linkedin) %></div>
            <br />
          <% end -%>
        </div>

        <div class="modal-footer">



  <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
     <%= f.error_notification %>

     <div class="form-inputs" style="padding-left:20%; text-align:left; color:black;">

       <%= f.input :first_name, placeholder: 'Enter your first name', autofocus: true, :input_html => {:maxlength => 15, :size => 40} %>
       <%= f.input :last_name, placeholder: 'Enter your surname', autofocus: true, :input_html => {:maxlength => 15, :size => 40} %>
       <%= f.input :email, placeholder: 'Enter email',  autofocus: true, :input_html => {:maxlength => 25, :size => 40} %>
       <%= f.input :password, placeholder: 'At least 8 characters', :input_html => {:maxlength => 15, :size => 40} %>
       <%= f.input :password_confirmation, placeholder: 'Confirm your password',  :input_html => {:maxlength => 15, :size => 40} %>
     </div>

     <div class="form-actions" style="padding-left:20%; padding-top:5%; text-align:left; color:black;">
       <%= f.button :submit, "Register" %><% end %>

I have a user modal which has validations for the presence of an email address and password.

I have a user/registration_controller with the following create function:

def create
  @user = User.new(user_params)#(params[:user])

  respond_to do |format|
    if @user.save
      # Tell the UserMailer to send a welcome email after save
      # AdminMailer.new_user_waiting_for_approval.deliver

      format.html { redirect_to(root_path, notice: 'Registration received.') }
      format.json { render json: root_path, status: :created, location: @user }
    else
      format.html { redirect_to(root_path, alert: 'Sorry! There was a problem with your registration. Please contact us to sort it out.') }
       # format.html { render action: 'new' }
       format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

I have devise helpers in both my application helper and devise helper as follows:

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end

My initializers file has two simple form files. One is simple_form.rb. The other is simple_form_bootstrap.rb

No errors display inline in the form. When I complete the form without an email address, I get the error that appears in the create function of my registration_controller.

I'd really like the error to appear inline, in the form, when the user clicks submit.

Does anyone know how to address this problem?

Thank you.

解决方案

Make sure you have similar line in ur initializer's wrapper

b.use :error, wrap_with: { tag: 'span', class: 'help-block' }

as you mentioned in ur question, please remove required: false, and to remove the '*' for required, change it in config/locale/simple_form.en.yml

required:
  text: 'required'
  mark: ''

这篇关于简单的表单错误通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆