简体   繁体   English

Rails 3-作为JS和Flash消息的response_to

[英]Rails 3 - respond_to as JS and flash message

After send a form I will process a data from form and wanna to render an action as JS. 发送表单后,我将处理表单中的数据,并希望将动作呈现为JS。 This work me. 这工作我。 But + this functionality I would like to add a flash message yet. 但是+此功能我想添加一个即显消息。 I try to do it as follows: 我尝试这样做,如下所示:

respond_to do |format|
  if @save.update_attributes(params[:data])   
    format.js {     
      flash[:notice] = 'The article was successfully updated.'
      render :update do |page|; page << "$('#my_div').html('#{escape_javascript(render(:partial => 'article'))}');" end
     }
    format.html {} 
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @data.errors, :status => :unprocessable_entity }
  end

Now the flash message is not displayed. 现在不显示闪存消息。 Could anyone help me, why not? 谁能帮我,为什么不呢? I tried the line with flash[:notice] move to out of format.js block, but the is still not displayed... 我尝试将带有flash[:notice]的行移出format.js块,但仍未显示...

Could you help me, please, why?r 你能帮我吗,为什么?

I guess you can use the point 2.2.10 of the documentation : 我想您可以使用文档的2.2.10点:

http://guides.rubyonrails.org/layouts_and_rendering.html#using-render http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

render :js => "$('#my_div').html('#{escape_javascript(render(:partial => 'article'))}');" 渲染:js =>“ $('#my_div')。html('#{escape_javascript(render(:partial =>'article'))}'););”

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

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