我试图弄清楚如何使用简单形式与Rails,Devise,Omniauth和Bootstrap。我有几个问题,但其中一个是错误通知。
我在Bootstrap模式中有一个设计登录和注册表单。登记表如下。请注意,我从表单中删除了required: true
函数,因为我不喜欢将星号附加到标签上(但我确实尝试将其放回以查看错误通知是否有效)。
<% 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">×</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 %>
我有一个用户模式,其中包含存在电子邮件地址和密码的验证。
我有一个user / registration_controller,其中包含以下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
我已经在我的应用程序助手和设计助手中设计了助手,如下所示:
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
我的初始化文件有两个简单的表单文件。一个是simple_form.rb。另一个是simple_form_bootstrap.rb
表格中没有错误显示内联。当我填写没有电子邮件地址的表单时,我收到了registration_controller的create函数中出现的错误。
当用户点击提交时,我非常希望错误在表单中显示内联。
有谁知道如何解决这个问题?
谢谢。
答案 0 :(得分:2)
有谁知道如何解决这个问题?
因为您能够使用registration
形式的对象,这意味着您对如何显示错误有一定的自由。如果您尝试使用login
(因为登录不使用表单)来实现相同的目标,那将是一个不同的故事
-
<强>表格强>
我们之前已经完成此操作(您可以看到here - 点击&#34;注册&#34;在顶部):
#app/views/devise/registrations/new.html.erb
<%= f.email_field :email, :placeholder => "email" %>
<div class="error"><%= devise_resource.errors[:email].first if devise_resource.errors[:email].present? %></div>
考虑到表单在devise_resource
对象中使用form_for
- 这应该允许您显示内联错误
答案 1 :(得分:2)
确保你的初始化程序包装器中有类似的行
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
正如您在问题中提到的那样,请删除required: false
,
并删除所需的'*',在config / locale / simple_form.en.yml中更改
required:
text: 'required'
mark: ''
答案 2 :(得分:1)
你说你在使用模态?但是你没有在你的表单上发布ajax(没有remote: true
)。所以我将描述我认为现在发生的事情。你没有发布ajax,所以你发布了html,所以它选择了format.html
路径,并且在出错时你的控制器重定向到root-path,并显示错误消息。相反,它应该重新呈现表单。
因此,取消注释创建操作中的注释行应该可以正常工作:)(并显然删除当前的format.html
行。
当然,重新渲染不会是一种模态。使用模式时,如果希望表单保持打开状态,则必须使用ajax并在出现错误时将表单替换为重新呈现的版本。
简单形式,当给定具有验证错误的对象时,将自动显示内联错误。
答案 3 :(得分:1)
如果出现以下情况,则会解决错误验证问题:
如果您对用户模型进行了验证并使用了设计验证,则表单顶部的错误列表将复制错误。删除一个或另一个验证,您将发现错误。
我无法弄清楚为什么简单的表单不会导致错误 - 但这是一个解决方案。
我决定从我的应用程序中删除简单的表单 - 我无法阅读文档,因此在Stack Overflow上发布的与设计中使用它有关的许多问题都没有得到解答。我看不出它的好处。
至于模态 - 我打算尝试使用它。 http://bootstrapvalidator.com(手指交叉)。我希望这个答案有助于某人。我一直在尝试设置3个月的设计 - 每周花费3个星期来努力理解文档。