加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:40:26 所属栏目:百科 来源:网络整理
导读:我是Rails的新手,并且已经在这个问题上苦苦挣扎了好几个小时 – 非常感谢任何帮助. 我在Bootstrap模式中有一个注册表单(使用Devise和simple_form),并希望在提交表单时在同一模式中显示错误通知.目前,当提交表单时出现错误,页面会重定向到/ users,并在那里呈
我是Rails的新手,并且已经在这个问题上苦苦挣扎了好几个小时 – 非常感谢任何帮助.

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

我不知道这个问题是否是由控制器/路由问题引起的,我是否需要(首先学习)添加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 SO问题后添加了此代码

module ApplicationHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

解决方法

你应该添加一个:remote =>对于您的表单,这会导致表单通过Ajax提交.然后,您需要将控制器修改为respond_to format.js.它应该尝试保存记录.如果有错误,它应该回复create.js.erb,其中包含以下内容:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读