【问题标题】:Rails Remote Link Processing as JS then HTMLRails 远程链接处理为 JS,然后是 HTML
【发布时间】: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


    【解决方案1】:

    原来%img{:src => "#"}/ 导致页面多次呈现。我删除了这条线,一切都按预期工作。如果您需要使用图像占位符,请使用= image_tag "",这会导致路由错误,但这比多次渲染页面要好得多。

    【讨论】:

      【解决方案2】:

      如果您使用的是 Rails 3+,则应该将 respond_to :html, :json, :xml 放在控制器顶部,然后在 show 操作中使用 respond_with @article。所以它可能看起来像:

      respond_to :html, :json, :xml
      
      def show
        article_name = params[:id].gsub('-', ' ')
        @article = Article.find_by_name(article_name)
        respond_with(@article)
      end
      

      它更清洁,更易于维护。

      我还建议您查看 to_param (http://apidock.com/rails/ActiveRecord/Base/to_param) 的文档,而不是您在其中的 gsub 代码,以通过文章的名称而不是 ID 查找文章。

      如果这没有帮助,我将不得不在下班后仔细查看,我实际上可以在测试应用程序中抛出一些代码并进行调试。

      【讨论】:

      • 谢谢乔恩!我进行了这些更改,但仍然遇到同样的问题。
      • 另外,您是否尝试过将 :format 显式传递给 link_to?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多