0

두 템플릿이있는 웹 사이트가 있습니다레일즈 플래시 메시지가 자바 스크립트로 실행되었습니다

하나의 템플릿 핸들은 정기적으로 플래시 메시지를받습니다. 다른 템플릿은 자바 스크립트를 통해 해고 메시지를 필요 :

var notyfy = notyfy(
{ 
    text: 'Hello I am an error', 
    type: 'error' // alert|error|success 
}); 

나는 flash_helper을 구축 할 수 있습니다.

  • 나는 view_helper 여기

내에서 자바 스크립트를 보내는 방법을 모르는 내 flash_helper.rb

있다 :이 도우미가 활성화 오른쪽 "플래시 메시지"를 다시 전송해야하는 템플릿 시계
# called via <%= flash_messages %> 

module FlashHelper 

    def flash_messages 
    return if flash.empty? 

    if controller.send(:_layout) == "pro/application" 
     flash.collect do |type, message| 
     # HERE SHOULD THE MAGICK HAPPEN :-) 
     javascript_tag "alert('All is good')", defer: 'defer' 
     end 
    else 
     flash.collect do |type, message| 
     content_tag(:div, :class => "notification-box notification-box-#{type}") do 
      content_tag(:p) do 
      content_tag(:i, nil, class: "icon-ok") + 
       message 
      end + 
       link_to("#", class: "notification-close notification-close-#{type}") do 
       content_tag(:i, nil, class: "icon-remove") 
       end 
     end 
     end.join("\n").html_safe 
    end 
    end 
end 

내 결과는 항상 일반 텍스트입니다 :

["<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>"]

,

답변

0

함께 작업하게되었습니다.

if controller.send(:_layout) == "pro/application" 
    html = "<script>" 
    flash.collect.with_index do |(type, message), i| 
    html += "var notyfy#{i} = notyfy({" 
    html += "text: '#{message}'," 
    html += "type: '#{type}'" 
    html += "});" 
    end 
    html += "</script>" 
    return raw(html) 
else 
.... 
... 
.. 
관련 문제