当我点击“检查”按钮而不重新渲染整个页面时,我正在尝试显示一条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'
答案 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类型的响应!