我的应用程序中有两种不同的布局,一种用于javascript (AJAX)请求,另一种用于常规请求。
# application.html.haml for non-js requests (abbreviated)
!!!
%html
%head
%body
= yield
# and application.js.coffee for js requests
App.modal """<%= yield %>""" # creates a javascript modal
理论上,任何与:remote => true
的链接都应该使用javascript布局。这在某些情况下是有效的,但在其他情况下不起作用。
它适用于此链接:
%li= link_to "Login", new_user_session_path, remote: true
# log output:
Started GET "/users/sign_in?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=" for 127.0.0.1 at 2012-01-27 03:29:41 -0500
Processing by Devise::SessionsController#new as JS
Parameters: {"authenticity_token"=>"KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI="}
Rendered devise/shared/_links.erb (0.9ms)
Rendered devise/sessions/new.html.haml within layouts/application (6.3ms)
Completed 200 OK in 167ms (Views: 165.9ms | ActiveRecord: 0.0ms)
# output in the javascript console:
XHR finished loading: "http://localhost:3000/users/sign_in?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=".
但对这个不起作用:
%li= link_to "Account", edit_user_registration_path, remote: true
# log output:
Started GET "/users/edit?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=" for 127.0.0.1 at 2012-01-27 03:31:24 -0500
Processing by Devise::RegistrationsController#edit as JS
Parameters: {"authenticity_token"=>"KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI="}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
MobilePhone Load (0.3ms) SELECT "mobile_phones".* FROM "mobile_phones" WHERE "mobile_phones"."user_id" = 1 LIMIT 1
Rendered devise/registrations/edit.html.haml within layouts/application (6.7ms)
Completed 200 OK in 157ms (Views: 154.4ms | ActiveRecord: 0.6ms)
# output in the javascript console:
XHR finished loading: "http://localhost:3000/users/edit?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=".
经过一些简单的调试,我意识到第二个请求正在命中application.html.haml
(错误!),而第一个请求正在命中application.js.coffee
(正确!)。ajax正在成功地处理这两个问题。
我在这里有点迷惑。我希望我犯了一个简单的错误,有人能指出!
谢谢!
附注:我正在运行rails 3.2.1 (之前在3.1.3上尝试过,但出现了同样的问题)
编辑:不确定是否有区别,但我使用的是mootools-rails驱动程序:https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js。accepts标头已正确设置为"text/javascript“。
发布于 2012-02-02 19:01:36
为了让它工作,我必须这样做:(与javascript库无关)
# whatever_controller.rb
def index
respond_to do |format|
format.js # renders index.html.haml inside application.js.haml layout
format.html # renders index.html.haml inside application.html.haml layout
end
end
或者,您可以只将操作的模板重命名为index.js.haml
而不是index.html.haml
,这样可以在不使用respond_to
的情况下处理ajax请求。然而,这将意味着没有启用javascript的搜索引擎和浏览器将无法访问该页面。
另一种同样有效的方法是使用respond_with
# application_controller.rb
respond_to :html, :js
# whatever_controller.rb
def index
respond_with # will render the appropriate layout
end
发布于 2012-08-25 23:09:24
我遇到了同样的问题,我从头开始尝试一个新的rails 3.2.7
,但由于我使用的是application.haml而不是application.html.erb,,远程链接无法工作。我尝试了下面的方法:
将application.haml转换为partial,将application.haml重命名为_application.haml
<%= render‘%>’应用程序
发布于 2012-01-27 09:49:31
至于布局,您应该在控制器上显式指定它。也许你可以将application.js.coffee改成另一个名字,并在控制器的顶部添加布局'new_name‘。
https://stackoverflow.com/questions/9030826
复制相似问题