简体   繁体   English

仅在Rails生产中使用ActionController :: UnknownFormat

[英]ActionController::UnknownFormat in Rails production only

I'm trying to display a flash message when I clicked the "check" button without re-rendering the entire page. 当我单击“检查”按钮时,我试图显示一条即显消息,而无需重新呈现整个页面。

In the development mode, the code below does work but doesn't in the production mode. 在开发模式下,下面的代码可以工作,但在生产模式下不起作用。 Instead of staying in the main.html and displaying the flash message, it redirect me to "check" page. 而不是停留在main.html上并显示Flash消息,而是将我重定向到“检查”页面。

The log console gave me debug messages below: 日志控制台为我提供了以下调试消息:

Completed 406 Not Acceptable in 2ms (ActiveRecord: 0.0ms) 在2ms内完成406不可接受(ActiveRecord:0.0ms)

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

Did I miss something for working on production mode? 我错过了一些在生产模式下工作的东西吗?

main.html.erb 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 _check.html.erb

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


check.js.erb check.js.erb

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


my_controller.rb my_controller.rb

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


routes.rb routes.rb

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

Try to the following 尝试以下

_check.html.erb _check.html.erb

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

check.js.erb check.js.erb

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

my_controller.rb my_controller.rb

Change this line 更改此行

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

Hope to help 希望对大家有帮助

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

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

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