gpt4 book ai didi

ruby-on-rails - 在模态中显示表单错误通知

转载 作者:行者123 更新时间:2023-12-04 06:09:53 24 4
gpt4 key购买 nike

我是 Rails 的新手,并且已经为这个问题苦苦挣扎了好几个小时 - 非常感谢任何帮助。

我在 Bootstrap 模式中有一个注册表单(使用 Devise 和 simple_form),并且希望在提交表单时在同一模式中显示错误通知。目前,当表单提交错误时,页面重定向到/users 并在那里呈现错误。当表单提交没有错误时,页面会停留在主页上,并根据需要显示“成功”闪烁消息。

我不知道这个问题是否是由 Controller /路由问题引起的,我是否需要(先学习)添加 jQuery/Javascript/Ajax 或其他东西。

模态代码(包含在部分中)

<div id="signupModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="signupLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="signupLabel">Sign up</h3>
</div>

<div class="modal-body">

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: "form-horizontal"}) do |f| %>

<%= f.error_notification %>

<%= f.input :name,
placeholder: 'John Smith',
input_html: {class: 'span7'} %>
...

<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-large btn-success" %>
</div>

<% end %>

</div>
</div>

application_helper.rb - 此代码是在阅读 this 后添加的所以问题
module ApplicationHelper
def resource_name
:user
end

def resource
@resource ||= User.new
end

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

最佳答案

您应该添加 :remote => true到您的表单,这会导致表单通过 Ajax 提交。然后您需要将您的 Controller 修改为 respond_to格式.js。它应该尝试保存记录。如果有错误,它应该回复 create.js.erb其中包含以下内容:

$("#errors_div").html("<%= @resource.errors.full_messages %>")

关于ruby-on-rails - 在模态中显示表单错误通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com