【发布时间】:2011-11-02 15:37:21
【问题描述】:
我有一个标记为:remote => true 的链接,它向Controller#show as JS 发出获取请求并在浏览器中呈现。在此请求完成后,处理 Controller#show as HTML 的另一个 get 请求。
在调试时,我将显示操作内容包装在 if request.xhr? && !request.format.html?
第一个Controller#show as js 请求在浏览器中正确显示,Controller#show as HTML 的意外呈现失败,显然没有任何东西进入浏览器。
我的问题是,有没有人在 js 调用之后经历过这个后续的 html 调用?我在我的代码中找不到任何导致此问题的内容。
链接代码
= link_to article.name, blog_path(article.name.downcase.gsub(' ','-')), :remote => true
控制器代码
def show
article_name = params[:id].gsub('-',' ')
@article = Article.find_by_name(article_name)
respond_to do |format|
format.html # show.html.erb
format.js
format.xml { render :xml => @article }
end
end
显示.js.haml 代码
$('#content_index.blog').html("#{escape_javascript(render('article'))}");
_article.html.haml 代码
.blog_post
%h2
= @article.name
%img{:src => "#"}/
.blog_text
%p
= @article.content
%center
.blog_post_bottom
#next.blogbuttons next
#previous.blogbuttons previous
%p
posted on
= link_to @article.created_at.to_s(:date_only), "#"
in
%a{:href => "#"} CS at work
by
= link_to @article.author_name, "#"
routes.rb 代码
resources :articles, :only => [:index, :show]
resources :blog, :controller => :articles
单个 js 请求的日志输出(浏览器中禁用 javascript 的 html 请求也会发生)
Started GET "/blog" for 127.0.0.1 at Thu Nov 03 14:38:29 -0400 2011
Processing by ArticlesController#index as JS
Article Load (9.3ms) SELECT `articles`.* FROM `articles`
Rendered articles/_articles.html.haml (2.3ms)
Rendered articles/index.js.haml (2.8ms)
Completed 200 OK in 20ms (Views: 3.5ms | ActiveRecord: 9.3ms)
Started GET "/blog" for 127.0.0.1 at Thu Nov 03 14:38:30 -0400 2011
Processing by ArticlesController#index as HTML
Article Load (9.7ms) SELECT `articles`.* FROM `articles`
Rendered articles/index.html.haml within layouts/application (2.7ms)
Rendered user_sessions/_new.html.haml (2.5ms)
Rendered shared/_header.html.haml (3.9ms)
Rendered shared/_footer.html.haml (0.8ms)
Completed 200 OK in 26ms (Views: 9.1ms | ActiveRecord: 9.7ms)
【问题讨论】:
标签: ruby-on-rails ajax ruby-on-rails-3 jquery