简体   繁体   English

通过js.erb文件呈现Flash消息

[英]Render the Flash message through js.erb file

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. 在我的Rails应用中,我想将Flash消息从控制器方法delete_optionquestions_controller_edit.html.erb到问题文件夹本身内的_edit.html.erb部分。 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 . 我有一个js文件delete_option.js.erb ,我可以使用以下行成功地将编辑呈现为id为edit_subquestion的div元素。

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

I put a div element inside the _edit.html.erb to render the flash message as given below. 我在_edit.html.erb放置了一个div元素,以如下所示呈现Flash消息。

<div id="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. 我正在尝试将flash消息从delete_option方法delete_option到上述div元素,如下所示。

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. 没有得到。

You can use ruby to do conditionals in your edit.js.erb file. 您可以使用ruby在edit.js.erb文件中进行条件检查。 Also, since you're inside the escape_javascript, you don't call render in there. 另外,由于您位于escape_javascript中,因此您无需在其中调用render。

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

Jesse Wolgamott : if loop was the problem. 杰西·沃尔加莫特(Jesse Wolgamott):如果循环是问题所在。 I modified it. 我修改了它。 now, its working. 现在,它的工作。

<% unless flash[:success].blank? %>
    $("div .success").html("<%= escape_javascript (raw(flash[:success])) %>");
    $("div .success").show(200);
    $("div .success").click(function(){
        $("div .success").hide(200);
    });
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM