How to return a the index.html.erb in the js.html.erb file

China☆狼群 提交于 2020-01-03 03:00:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!