轨道3 - 后阿贾克斯的形式留页面上提交,但秀"闪光" [英] rails 3 - stay on page after ajax form submit but show "flash"

查看:81
本文介绍了轨道3 - 后阿贾克斯的形式留页面上提交,但秀"闪光"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Rails 3,我有一个Ajax的形式(如:远程=>真正的)。这打在我的控制器中的插入行动

I'm using Rails 3 and I have an ajax form (i.e. :remote => true) that hit's a "insert" action on my controller.

我要的是为用户留在索引页上,但能够显示在控制器中的插入动作设置提示信息。

What I want is for the user to stay on the index page but be able to display flash messages set by the "insert" action in the controller.

目前我的插入行动确实是这样的:

Currently my "insert" action does something like:

def insert
  begin
    # perform insert of stuff
    flash[:success] = "Successfully..."
  rescue
    flash[:error] = "failed ..."
  end
  render :nothing => true
end

这使形式(上index.html.erb)应提交,并保持在该网页的用户,但没有闪光灯的信息显示出来。我怎样才能出现的提示信息? (猜测它做的渲染:没有什么东西,但没有我得到的服务器日志缺失模板的错误)。

This allows the form (on index.html.erb) to be submitted and keeps the user on that page but no flash messages show up. How can I get the flash messages to appear? (guessing it has to do with the render :nothing stuff, but without that I get "missing template" errors in the server log).

推荐答案

您将需要一个JavaScript应答:

You'll need a javascript responder:

def insert
  begin
    # perform insert of stuff
    flash[:success] = "Successfully..."
  rescue
    flash[:error] = "failed ..."
  end
end


# insert.js.erb
$('#flash').html(<%= flash[:notice].to_json %>);

使用闪光灯展现形式的错误消息肯定是不规律的行为。你会更好地与错误信息的表单中更新的元素或完全替代形式。

Using flash to show form error messages is definitely irregular behavior. You'd be better off updating an element within the form with the error message or replacing the form altogether.

这篇关于轨道3 - 后阿贾克斯的形式留页面上提交,但秀&QUOT;闪光&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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