Loading controller specific code - ruby-on-rails

<%= javascript_include_tag "application", params[:controller] %>
I added the above tag in my application.html.erb, but it seems my js files are not loaded according to controller. When i navigate to localhost:8000/sessions it does not load sessions.js

Rails now uses the asset pipeline. In app/assets/javascripts/application.js you can require all your of javascript files with "magic comments":
//= require_tree .
Your javascript can now be inside app/assets/javascripts/sessions.js or even app/assets/javascripts/sessions.js.coffee if you want to use coffeescript.
Note that this will require all Javascript files on every page, which is often a desired effect because the client can cache the javascript and thus only has to download it at the first request. For more information read the Rails Guide I linked to above.

You can load controller specific js files as follows
<%= javascript_include_tag "application", controller_name %>
So that, if you navigate to localhost:8000/sessions it will load sessions.js file. And it is not necessary to add //= require_tree . in application.js.

Related

Rails: Not requiring js files to load on homepage

I have a rails application and the homepage is mostly a static page with some jquery. Once the user logs in there are other javascript files that are being used.
However, when I load the homepage I can see that all the js files are being loaded.
Is there a way not to load them? Should I remove //= require_tree . from application.js and somehow load them only when they are needed on other pages? I guess that should improve the loading time and Google pagespeed insights.
Thanks!
Removing //= require_tree . would stop loading all js in the tree. However, we have no idea what "the home page" is or what the rest of your application will need the js for. If you have a controller end point i.e. HomeController#index you might just wanna use a different layout for that controller/action and omit the //= require_tree from that layout. Create a separate layout file, you could copy application.html.erb to views/layouts/home.html.erb for example and omit the line
<%= javascript_include_tag 'application' %>
Then something like this in the controller:
class HomeController < ApplicationController
layout 'home'
end

JS and CSS file for each view Phoenix Elixir

I am finding few ways to get my work routine good with Phoenix as I have with rails, There are some perspectives which I am looking in Phoenix are.
1: In rails, I have my controller and my view, also the CSS and JS files alongside, When I want to include CSS files only in one relevant view, I only do as
<%= yield :head %>
and In my View file, I only do
<% content_for :head do %>
<%= stylesheet_link_tag "views/index/landing.css" %>
<% end %>
That's it. My CSS file is only loading for my relevant view.
2: For JS, I have application.js file in which I included my other libs and in the end, I have admin.js in which I have included my every other file as
//= require jquery
//= require dataTables/jquery.dataTables
//= require jquery_ujs
//= require bootstrap.min
//= require admin/admin.js
admin.js.coffee file
#= require admin/sign_up.js.coffee
#= require admin/company.js.coffee
#= require admin/user.js.coffee
3: For every JS file What I am doing is, e.g for Signup view's JS file I have done as under, in the very end of the file,
window.initializeSignUp = ->
onSignUp()
after this What I only need in my sign up view is
<script>
$(document).ready(function () {
window.initializeSignUp();
});
</script>
Now it's only calling that method of Signup and everything is working fine.
On the other hand, I love Elixir it's a very good and lovable language to work in, BUT whenever I start project in it, I already got afraid of Brunch and how Assets are going to be handled.
for those who will read the question and will just downvote without even understanding the main issue here: Am a newbie to Phoenix for creating an APP with views and assets, etc.
I just need a clear and correct answer to work with Phoenix without using brunch or any web-pack solution, But a normal way, Is that possible in possible?
I believe your question has been answered here. The answer shows how to add page specific javascript but the same technique can be done for your css styles.
Here's a good explanation of how templates/views work in Phoenix.

Rails add a lightbox to an application

Rails 4.
Hi I d'like to add an lightbox to my application. I have follow the step-by-step instructions for Lightbox but my photo gallery doesn't respond at all
I have called (:all : application) the file necessary in my application.html.erb. My view is ok I think the problem reside in my way to call the vendor files.
I place all the lightbox files under vendor/assets/javascript and vendor/assets/stylesheets respectively. I did create an vendor/assets/images for the Lightbox images file (close icon...) and my application.html.erb contains:
stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
javascript_include_tag "application", "data-turbolinks-track" => true
Nothing show up properly concerning my photo-gallery. any help?
in your /assets/javascripts/application.js add this line after //= require jquery:
//= require lightbox-2.6.min
same thing for your /assets/stylesheets/application.js add this line :
//= require lightbox
I added the following line that #medBo told me too.
But for the IMG folder I placed it inside the public folder.
add '.scss' to the lightbox.css file and edit
line 195 to 'background: url(/img/close.png) top right no-repeat;'
then you can keep the img folder inside the assets folder where it belongs if you are going to compile assets localy
Probably you can take a look at these gems:
Lightbox Rails
Lightbox for Bootstrap 3

How to load page specific rails 4 js files?

I am reading rails guides documentation for asset pipeline.
It states that coffeescript page specific generated files
are by default ready to user if there is a require_tree directive on the manifest.
This is not working with me I have to do include this
<%= javascript_include_tag params[:controller] %>
on the specific controller.
What am I missing ?
The asset pipeline will compress all of your JS into a single file, application.js. In order to call JS for a specific page, you will need to organize your JS by controller and action. There is a gem, RailsScript that does this automatically and it's compatible with Turbolinks which can give you a single page application feel.
RailsScript only takes a few minutes to learn, https://github.com/gemgento/rails_script.
A specific example using rails script:
# app/assets/javascripts/users.js.coffee
window.App ||= {}
class App.Users extends App.Base
show: ->
alert('The users#show action!')
I think you are misunderstanding the asset-pipeline in general. It doesn't load the javascript-files individually, but rather all the .js.coffee files will get compiled into one big js-file, which you have to include in your views/layout like this
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
If you want some js-code that is only available in one view, you definitely should not include that into the asset-pipeline.
Not sure if I've misunderstood your first paragraph, but I think what the line means is that if your application.js manifest contains a line like:
//= require_tree .
Then yes indeed, page specific javascript, or coffeescript will be loaded, not only for that specific page, for for all pages. If you want to constrain assets to certain pages like you've described, you will need a file located in app/assets/javascripts/ with the pluralized name of the controller, and .js.
I would personally create this as another manifest for that specific page, that way I can load multiple assets. Lets say you have a controller called UsersController, with various assets used by that controller's views. What you then need, in order for the line you wrote in your question to work, is a .js filed users.js or users.js.coffee in the app/assets/javascript directory.
Alternatively, to maintain the naming convention, I do something like this:
<%= javascript_include_tag "application-#{params[:controller]}"%>
and then of course name my file appropriate (application-users.js).
Also, when you do this, you'll want to stop your page-specific assets from loading for all controllers. Simply remove the //= require_tree . line and replace it with explicit //= require lines as needed.
Here's a way to do page-specific javascript in rails.
Install the jquery-readyselector.js plugin. (It's 18 lines)
a. Copy the contents of https://raw.github.com/Verba/jquery-readyselector/master/jquery.readyselector.js
b. Paste the contents to a new file at assets/javascripts/jquery_readyselector.js
c. Require jquery-readyselector
// assets/javascripts/application.js
//= require jquery_readyselector
//= require_tree .
Create CSS classes so we have a way to reference each page individually.
<%# views/layouts/application.html.erb %>
<body class="<%= controller_name %> <%= action_name %>">
Now we can scope our javascript to our page using CSS.
// assets/javascripts/posts.js
$(".posts.index").ready(function() {
});

Where does application.js code go in rails 3.1?

Prior to rails 3.1, javascript code that was common to the application belonged in application.js by default, and was loaded by javascript_include_tag :defaults
With the asset pipeline in rails 3.1, the application.js file becomes a manifest file, and it appears that code I put in it is not included in the result. Where is this javascript code supposed to be moved to now? Obviously, I could create any other name and make sure that it is included by the manifest, but is there a default location already expected by idiom?
I encounter the same problem in rails 3.1 rc6. I use javascript_include_tag :application instead
Look closer:
the code in application.js is rendered, it's just at the end of the resulting js file.
Example, try:
//= require jquery
//= require jquery_ujs
//= require_tree .
alert('foo');
the alert dialog will appear in all pages.

Resources