1

I want to add a JavaScript on one view page, how can i do that? I'm using rails 4 in my application.

I have tried this but it is not working, in my view

<script type="text/javascript">
    $(function() {
      $('user_button').click(function() {
        $('some_div').show();
      }
    });
  </script>
1
  • Rails 3 or 4? Why are you trying to put this in your view? Use assets/javascripts. Commented Nov 4, 2013 at 9:42

1 Answer 1

1

this always works, make use of yield.

in application.html.erb, add:

<%= yield :head %>

then in your view page where you want to add javascript:

<% content_for :head do %>
  <script type="text/javascript">
    $(function() {
      //script here
      }
    });
  </script>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments