简体   繁体   中英

Rails: erb + javascript question

Can I have conditional javascript in my show.erb.html files?

Something like this:

<%- if File.exists?("/leggle/data/tree_xml.xml") %>
  <script type="text/javascript"> 
    ...code...
  </script>
<%- end %>

and have the javascript code appear only if that file exists??

Yes you can. The better way to do it though is to use unobtrusive JavaScript (UJS) which will keep JS and HTML separated (just like Model/View/Controller). In your case you'll either have or not have a tag (for example) with a JavaScript with assigned action to it.

Yes. With something like this, it's always easy to just try it out, visit the page, and check the page source to see if it's there (or see if the JS action happened).

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