仅在Rails生产中的ActionController :: UnknownFormat

时间:2018-01-13 15:33:05

标签: ruby-on-rails

当我点击“检查”按钮而不重新渲染整个页面时,我正在尝试显示一条Flash消息。

在开发模式下,以下代码可以正常工作,但不在生产模式下。

,而不是留在main.html中并显示Flash消息,而是将其重定向到“检查”页面。

日志控制台给了我下面的调试消息:

  

已完成406在2ms内无法接受(ActiveRecord:0.0ms)

     

ActionController :: UnknownFormat(ActionController :: UnknownFormat):
  app / controllers / my_controller.rb:105:在`check'

我是否因为在生产模式下工作而错过了什么?

main.html.erb

<div class="check"><%= render partial: 'check' %></div>
<%= f.submit :Check, class: "btn btn-default", remote: true, formaction: app_check_path %>


_check.html.erb

<div id = "notice">
</div>


check.js.erb

$("#notice").html('<p style="color: green;"><%= flash[:notice] %></p>').show().delay(5000).hide(0);


my_controller.rb

def check
   notice = "NOTICE!"
   respond_to do |format|
       format.js do
          flash[:notice] = notice
       end
   end
end


的routes.rb

get 'app/check'
post 'app/check'

2 个答案:

答案 0 :(得分:0)

尝试以下

<强> _check.html.erb

<% flash.each do |msg| %>
    <%= msg.html_safe %>
<% end %>

<强> check.js.erb

$("#notice").html('<%= escape_javascript(render("check")) %>');

<强> my_controller.rb

更改此行

notice = "<p style='color: green;'>NOTICE!</p>"

希望能提供帮助

答案 1 :(得分:0)

您需要以JS格式提交表单才能呈现JS类型的响应!