简体   繁体   中英

Stop Rails 3.2 from rendering both html.erb and js.erb

I'm calling an action, Home#index , to an empty action in HomeController .

The page loads fine in HTML, which is what I expect (rendering home/index.html.erb , but then it also loads the corresponding js for that action as well, arriving some 100ms after (rendering home/index.js.erb ). How can I stop this? I only want the javascript response when I call for it, not through an HTTP GET request.

HomeController

class HomeController < ApplicationController
  respond_to :html, :js # Nothing changes whether I have this or not

  def index
    respond_to do |format| # Nothing changes whether I have this or not
      format.html
      format.js
    end
  end
end

Routes.rb

Ajaxtest::Application.routes.draw do
  get "home/index"
  root to: 'home#index'
end

index.html.erb

<p>Find me in app/views/home/index.html.erb.</p>

index.js.erb

$("#content").html("Lorem ipsum dolor sit amet, yo non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
document.title = "AJAX loaded";

从控制器的respond_to块中删除format.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM