在不重定向的情况下在 Rails 控制器中发出警报/通知 [英] Raise an alert/notice in rails controller WITHOUT redirect

查看:43
本文介绍了在不重定向的情况下在 Rails 控制器中发出警报/通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果电子邮件未保存/未保存,我想显示通知/错误,不使用重定向.我在项目中使用 Rails 4,这是我的代码:

layouts/application.html.erb

<头><title>FarFlung Jobs</title><!--/.您也可以将 style.css 包含到 stylesheet_link_tag 中.如果这样做,不要忘记在asset.rb中添加style.css --><%= stylesheet_link_tag 'application', 'jobs', media: 'all' %><%= javascript_include_tag '应用程序' %><%= javascript_include_tag 'config', 'sharebutton', 'jobsalert', 'modernizr'%><%= 渲染 'payola/transactions/stripe_header' %><%= csrf_meta_tags %><身体><%= 渲染共享/导航栏"%><div><% flash.each do |name, msg|%><%= content_tag :div, msg, class: 'alert alert-info' %><%结束%>

<%=产率%><%=渲染'共享/页脚'%></html>

users/new.html.erb

<div class="column panel panel-default"><div class="cd-filter"><h4>订阅工作提醒</h4></div><%= simple_form_for User.new do |f|%><%= f.input :email, label: false, :placeholder =>'输入您的电子邮件地址...', :input_html =>{ :class =>'时事通讯表单字段样式' } %><%= f.button :submit, 'SUBMIT', :class =>'btn-block newsletter-form-styling btn-primary submit'%><%结束%>

</节>

users_controller.rb

class UsersController <应用控制器定义新@user = User.new结尾定义创建@user = User.new(secure_params)如果@user.valid?@user.saveflash.now[:notice] = "#{@user.email} 已注册职位提醒."别的flash.now[:alert] = '订阅错误!'结尾结尾私人的def secure_paramsparams.require(:user).permit(:email)结尾结尾

如何在不重定向页面的情况下让 Rails flash 消息显示在我的订阅表单上?

解决方案

您可以通过 remote: true 提交表单.它会(如您所料)远程提交您的表单,因此您可以返回一个 json 并呈现预期的 flash.例如:

您的表单:

simple_form_for User.new, remote: true, class: 'your-form' do<你的代码>

您的控制器

def 创建@user = User.new(secure_params)如果@user.saverender json: { status: 'success', message: "#{@user.email} 已注册工作提醒."}别的渲染json:{状态:'失败',消息:'订阅错误!'}结尾

结束

您的 JS(可能是 new.js - 并确保将其包含在您的视图中)

//你可以指定你的bind容器,这里我只用了文档$(document).on('ajax:success', '.your-form', function(e, data) {if(data.status == 'success'){showSuccessFlash(数据);}别的{showErrorFlash(数据);}});

说明:

使用 remote: true,您的页面将等待 ajax 应答,您可以监听 ajax:success(或其他绑定).

然后,它将接收json 并将其存储在data 变量中.因此,您将获得 data.statusdata.message,您可以将它们作为反馈显示给您的用户.

有关 remote: true 的更多信息 http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#form-for

有关 JS 回调的更多信息 https://github.com/rails/jquery-ujs/wiki/ajax

I want to flash a notice/error if the email is/isn't saved, without using a redirect. I am using Rails 4 for the project, and here is my code:

layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>FarFlung Jobs</title>
  <!-- /.You can include style.css into stylesheet_link_tag too. If you do so, dont forget to add style.css in asset.rb -->
  <%= stylesheet_link_tag    'application', 'jobs', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= javascript_include_tag 'config', 'sharebutton', 'jobsalert', 'modernizr'%>
  <%= render 'payola/transactions/stripe_header' %>
  <%= csrf_meta_tags %>
</head>
<body>
    <%=  render 'shared/navbar' %>

    <div>
      <% flash.each do |name, msg| %>
        <%= content_tag :div, msg, class: 'alert alert-info' %>
      <% end %>
    </div>

<%= yield %>
    <%=  render 'shared/footer' %>
</body>
</html>

users/new.html.erb

<section class="cd-form-wrapper cd-container">
    <div class="column panel panel-default">
      <div class="cd-filter"><h4>SUBSCRIBE FOR JOBS ALERT</h4></div>
      <%= simple_form_for User.new do |f| %>
          <%= f.input :email, label: false, :placeholder => 'Enter your email address...', :input_html => { :class => 'newsletter-form-field-styling' } %>
          <%= f.button :submit, 'SUBMIT', :class => 'btn-block newsletter-form-styling btn-primary submit' %>
      <% end %>
    </div>
</section>

users_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(secure_params)
    if @user.valid?
      @user.save
      flash.now[:notice] = "#{@user.email} is signed up for Jobs Alert."
    else
      flash.now[:alert] = 'Error Subscribing!'
    end
  end

private

  def secure_params
    params.require(:user).permit(:email)
  end
end

How do I get the Rails flash message working to appear on my subscription form without redirecting the page?

解决方案

You can submit your form passing remote: true. It'll (as you can expect) remotely submit your form, so you can return an json an render the expected flash. Ex:

Your form:

simple_form_for User.new, remote: true, class: 'your-form' do 
  <your code>

Your controller

def create
@user = User.new(secure_params)
if @user.save
  render json: { status: 'success', message: "#{@user.email} is signed up for Jobs Alert." }
else
  render json: { status: 'failure', message: 'Error Subscribing!' }
end

end

Your JS (perhaps new.js - and be sure to include it in your view)

// you can specify your bind container, here I just used the document
$(document).on('ajax:success', '.your-form', function(e, data) {
  if(data.status == 'success'){
    showSuccessFlash(data);
  }else{
    showErrorFlash(data);
  }
});

Explaining:

Using the remote: true, your page will wait for an ajax answer, which you can get listening to ajax:success (or other bindings).

Then, it will receive the json and will store in the data variable. So you will get the data.status and the data.message, which you can show to your user as a feedback.

More infor about the remote: true http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#form-for

More info about the JS callbacks https://github.com/rails/jquery-ujs/wiki/ajax

这篇关于在不重定向的情况下在 Rails 控制器中发出警报/通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆