简体   繁体   中英

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.

The log console gave me debug messages below:

Completed 406 Not Acceptable in 2ms (ActiveRecord: 0.0ms)

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

Did I miss something for working on production mode?

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'

Try to the following

_check.html.erb

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

check.js.erb

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

my_controller.rb

Change this line

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

Hope to help

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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