简体   繁体   中英

Rendering html in rails 3

I am using Rails 3.2.1 and ruby 2.1.5.

In one of my controller I need to render a HTML line where I will include a source for a javascript file. much like the below code.

  def sample_application
     render :html => "<script type='text/javascript' src='http://js.live.net/v5.0/wl.js'></script>"
  end

I know the above syntax is horribly wrong.

what should I change above so that I can include the Javascript source by rendering.

try this

def sample_application
    respond_to do |format|
        format.js { render 'sample_application.js.erb' }
    end
end

and in sample_application.js.erb you can put <script type='text/javascript' src='http://js.live.net/v5.0/wl.js'></script>

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