3
私はアクションHome#index
を空のアクションHomeController
に呼び出しています。Rails 3.2がhtml.erbとjs.erbのレンダリングを停止する
ページがHTMLで正常に読み込まれます(レンダリングhome/index.html.erb
ですが、そのアクションの対応するjsもロードされ、100ms後に到着します(レンダリングhome/index.js.erb
)。私がいないHTTP GETリクエストによって、それを呼び出したときにのみjavascriptの応答をしたい。
に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";
これを行うと、HTTP GETリクエストを介してhtmlに応答しなくなります。しかし、提案をありがとう! – joseph