问题
I have a controller like so:
def index
respond_to do |format|
format.html {}
format.js {render :layout => false}
end
end
index.html.erb
hello i"m index.html.erb
index.js.erb
<%= render :template => "dashboard/index" %>
What I want to do is have index.html or index.js return the same thing. But I can't see to do that without making index.html.erb a partial (_index.html.erb) but then when a user makes an html call that won't work.
How can I get the index.js.erb to render index.html.erb? which I would then escape with jQuery. Thanks
回答1:
Try:
<%= raw render(:template => "dashboard/index").to_json %>
回答2:
I have this code in my application to do it:
<%= escape_javascript(render :file => "/posts/index.html.erb") %>
No sure if it's a "right" way, but it definitely works.
来源:https://stackoverflow.com/questions/6260418/how-to-return-a-the-index-html-erb-in-the-js-html-erb-file