Rails Remote link processing as JS, then HTML

I have a link labeled :remote => true , which makes a request to receive Controller#show as JS and displays in the browser. Upon completion of this request, another receive request is processed for Controller#show as HTML .

During debugging, I wrapped the contents of the show action in if request.xhr? && !request.format.html? if request.xhr? && !request.format.html?

The first Controller#show as JS request was correctly displayed in the browser, and the inadvertent rendering of Controller#show as HTML failed, and nothing made it explicit in the browser.

My question is: did anyone experience this subsequent html call after js call? I cannot find anything in my code causing this.

Link Code

 = link_to article.name, blog_path(article.name.downcase.gsub(' ','-')), :remote => true 

Controller code

  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 code

 $('#content_index.blog').html("#{escape_javascript(render('article'))}"); 

_article.html.haml code

 .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 code

  resources :articles, :only => [:index, :show] resources :blog, :controller => :articles 

Log output from one js request (also happens for html request with javascript disabled in the browser)

 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) 
+4
source share
2 answers

It turns out that %img{:src => "#"}/ causes multiple page rendering. I deleted this line and it worked as intended. If you need to use an image holder, use = image_tag "" , this will result in a routing error, but much better than displaying the page several times.

+1
source

If you use Rails 3+, you should instead put respond_to :html, :json, :xml respond_with @article respond_to :html, :json, :xml at the top of your controller, and then in the show action use respond_with @article . So it might look like this:

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

It is much cleaner and easier to maintain.

I also suggest that you look at the documentation for to_param ( http://apidock.com/rails/ActiveRecord/Base/to_param ) instead of the gsub code that you have to find the article by name, not identifier.

If this does not help, I will need to take a closer look later after work, where I really can throw some code into the test application and debug it.

0
source

Source: https://habr.com/ru/post/1379334/


All Articles