加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails远程链接处理为JS然后是HTML

发布时间:2020-12-17 02:00:28 所属栏目:百科 来源:网络整理
导读:我有一个标记的链接:remote =如果对Controller#的get请求显示为JS并在浏览器中呈现,则为true.在此请求完成另一个Controller#的get请求后,将处理 HTML. 在调试时我将show动作内容包装在request.xhr中? 安培;安培; !request.format.html? 第一个Controller
我有一个标记的链接:remote =>如果对Controller#的get请求显示为JS并在浏览器中呈现,则为true.在此请求完成另一个Controller#的get请求后,将处理 HTML.

在调试时我将show动作内容包装在request.xhr中? &安培;&安培; !request.format.html?

第一个Controller#show as js request在浏览器中显示正确,并且Controller#的意外呈现显示为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

show.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)

解决方法

结果证明%img {:src => “#”} /导致页面多次渲染.我删除了这一行,一切正常.如果你需要使用图像占位符use = image_tag“”这将导致路由错误,但这比页面呈现多次好多了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读