通过js.erb文件呈现Flash消息 [英] Render the Flash message through js.erb file

查看:71
本文介绍了通过js.erb文件呈现Flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用中,我想将Flash消息从控制器方法delete_option(questions_controller)渲染到问题文件夹本身内的部分_edit.html.erb.我有一个js文件delete_option.js.erb,我可以使用以下行成功地将编辑呈现为ID为edit_subquestion的div元素.

In my Rails app, I want to render the flash message from my controller method delete_option (questions_controller) to a partial _edit.html.erb inside questions folder itself. I have a js file delete_option.js.erb and I could successfully render the edit using the below line to a div element with id edit_subquestion.

$("#edit_subquestion").html("<%= escape_javascript(render(:partial => "edit")) %>");

我在_edit.html.erb中放置了一个div元素,以如下所示呈现Flash消息.

I put a div element inside the _edit.html.erb to render the flash message as given below.

<div id="flash_delete_option"></div>

我正在尝试将Flash消息从方法delete_option呈现到上面的div元素,如下所示.

I am trying to render the flash message from the method delete_option to the above div element as given below.

if((<%=flash[:success]%>).length > 0)
    $("#flash_delete_option").html("<%= escape_javascript(render('<%=flash[:success]%>')) %>");

我遇到语法错误.请帮助解决此问题.一切都尝试按照答案.没有得到.

I am getting syntax errors. Please help to resolve this. Everything tried as per the answer. Not getting it.

推荐答案

您可以使用ruby在edit.js.erb文件中执行条件操作.另外,由于您位于escape_javascript中,因此您无需在其中调用render.

You can use ruby to do conditionals in your edit.js.erb file. Also, since you're inside the escape_javascript, you don't call render in there.

<% if flash[:success] %>
    $("#flash_delete_option").html("<%= escape_javascript flash[:success]%>");
<% end %>

这篇关于通过js.erb文件呈现Flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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