Topic: javascript load issue

I've generated a controller with the command:

rails generate controller pages login registration

I want to create a javascript named "login.js" to validate login page and "registration.js" to validate registration page

I'm new to Ruby on Rails

I created the "login.js" file in /assets/javascript/login.js

but this file is being loaded for both "login" and "registration" page

I've searched through many Q&A on net but i found too difficult to have an understanding

Please somebody tell me what wrong i'm doing

Re: javascript load issue

I found the solution:


To add page specific css / javascript, create following folders in public

     scripts , css    #or any name

     add your css & scripts in the above folders
     Now add following lines in "application.html.erb" file:

     <%= yield :head %>       # inside the head section

     In the specific page, add the following lines:

     <% content_for :head do %>
      <script type="text/javascript" src="scripts/login.js"></script>
      <link rel="stylesheet" type="text/css" href="css/login.css" />
     <% end %>

Re: javascript load issue

You can add it without content_for, but it will load not in the head part.
Also check these helpers:

<%= javascript_include_tag "scripts/login" %>
<%= stylesheet_link_tag "css/login" %>

Re: javascript load issue

ashutoshpandey wrote:

I found the solution:


To add page specific css / javascript, create following folders in public

     scripts , css    #or any name

     add your css & scripts in the above folders
     Now add following lines in "application.html.erb" file:

     <%= yield :head %>       # inside the head section

     In the specific page, add the following lines:

     <% content_for :head do %>
      <script type="text/javascript" src="scripts/login.js"></script>
      <link rel="stylesheet" type="text/css" href="css/login.css" />
     <% end %>

good job!

i am a rails rookie