Rails 3 - including JavaScript in a view?

here is what i am trying to do correctly .... When a user subscribes and redirects to the landing page, I want to run a bit of jquery ajax to grab and paste some content.

I originally had this in my application.js file, but the problem is that the code for a user who is not logged in means that I have to add logic to account for what doesn't look clean.

What I need is when a call is called to enable JavaScript. I tried to insert it into the file of the view itself, but it did not succeed because it was loaded before the JQUERY, which is located at the bottom of the page ...

How do gurus handle this Rails 3? Thank you

+3
source share
3 answers

Well, you can include the jQuery file at the top.

If this is not an option, add <%= yield :javascript %>jQuery to the layout below and run:

<% content_for :javascript do %>
    <script type="text/javascript">
        // code goes here
    </script>
<% end %>

It will be placed in the right place in your layout. (However, I would recommend, for caching, that you write Javascript in an external file and include it where necessary, instead of using the built-in Javascript. Just good practice.)

+5
source

Download jQuery from the Google CDN and load your JS view of a specific view after. If you are worried about landing page speed for users who don’t subscribe to it, try using this Cached page.

, -, Sqiud. Jammit Asset Packager (http://synthesis.sbecker.net/pages/asset_packager), javascript CSS.

, , , .

+2

, jQuery . API Google, , , .

+1

Source: https://habr.com/ru/post/1766558/


All Articles