2012-01-27 26 views
0

我在我的应用中有两种不同的布局,一种是针对javascript(AJAX)请求,另一种针对常规请求。Rails没有为javascript请求选择正确的布局

# 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 "https://stackoverflow.com/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 "https://stackoverflow.com/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成功处理。

我在这里有点难住。我希望我犯了一个简单的错误,有人可以指出!

谢谢!

P.S.我正在运行rails 3.2.1(以前在3.1.3上试过同样的问题)

编辑:不知道它是否有所作为,但我使用的是mootools-rails驱动程序:https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js。接受标题正确设置为“text/javascript”。

回答

0

这是我必须做的就是这个工作:(并没有任何与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 
0

至于布局,你应该在控制器上明确指定它。也许你可以把名字application.js.coffee改成别的名字,并在控制器的顶部添加layout'new_name'。

+0

谢谢,我可以尝试,但不应轨根据请求标题自动选择合适的布局?请求标头当前设置为“text/javascript”(使用mootools rails驱动程序),但它使用错误的布局。另外我在控制器的顶部添加了respond_to:js,但这也没有帮助。 – mattwindwer 2012-01-27 20:18:22

+0

另外,如果用户没有启用javascript,或者对于搜索引擎,我希望application.html.haml作为后备的布局 – mattwindwer 2012-01-27 23:42:00

0

如果你正在使用jQuery UJS然后尝试加入

data-type="script" 

属性的链接标签。这将设置jQuery请求的dataType,然后设置您的http请求的Accepts:标头。 Rails使用它来猜测您的请求的适当响应类型。

看到这个博客帖子的一些背景资料:http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/

+0

好点,但实际上我使用的是Mootools UJS驱动程序(https:/ /github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js) – mattwindwer 2012-01-27 19:52:43

2

我有同样的问题,我试图用一个新的rails 3.2.7从无到有,为我所用application.haml,而不是应用.html.erb,远程链接无法正常工作。我想这项工作围绕:

  1. 转换application.haml到局部,重命名application.haml_application.haml
  2. application.html.erb只有一条线路:

    <%= render 'application' %>