当我点击 link1 (Ajax请求)时,我收到ActionController::UnknownFormat
。不确定为什么它不会生成js
输出。
根据Rails日志,它正在生成HTML!
Started GET "/sp/4/link1.html" for 127.0.0.1 at 2015-12-18 14:07:09 +1100
Processing by PagesController#show as HTML
Parameters: {"id"=>"4", "other"=>"link1"}
Rendered pages/show.html.erb within layouts/application (0.2ms)
Rendered shared/_leftnavigation.html.erb (0.2ms)
Completed 200 OK in 6ms (Views: 6.0ms | ActiveRecord: 0.0ms)
pages_controller.rb
class PagesController < ApplicationController
def index
end
def show
respond_to do |format|
#format.html
format.js
end
end
end
_leftnavigation.html.erb
<div class="panel-body">
<%= link_to "link1(with Ajax)", root_url+"sp/4/link1.html", remote:true %>
</div>
<div class="panel-footer">
<%= link_to "link2(no Ajax)", root_url+"sp/3/link1.html" %>
</div>
show.html.erb
<%= "<b> showproduct page...... </b>" %>
show.js.erb
$('#replacehere').html('<%= j render ("show") %>');
注意:#replacehere
layout/application.html.erb
您可以查看我的github repo以获取完整的源代码。 的 https://github.com/stackaccount/test
它有一个页面控制器,当用户点击link1( app / views / shared / _leftnavigation.html.erb )时,它应该加载show.js.erb
并显示show.html.erb
。< / p>
format.html
没有任何问题,但format.js
没有问题。
过去两天我一直在努力解决这个问题。请帮忙。
答案 0 :(得分:1)
确保jquery_ujs
application.js
//= require jquery
//= require jquery_ujs
目前,您的链接以.html
结尾,强制您的控制器以html
回复。因为您注释掉了format.html
块,因此引发了异常。要解决这个问题,我认为你有两种方法:
.html
format: 'js'
link_to
醇>