I have a stylesheet, application.css defined in layouts/application.html.erb:
<%= stylesheet_link_tag "application" %>
However, there's a section of the site where the views will use a completely different stylesheet, dashboard.css which I've defined in its index.html.erb:
<head>
<title>My title</title>
<%= stylesheet_link_tag "dashboard" %>
..
Unless I remove the stylesheet_link_tag in the application layout file, there are conflicts which make the dashboard view weird. If I move the application layout stylesheet tag to a _header.html.erb partial which is rendered with every view in the non-dashboard section like below, it doesn't work. How must I call them?
<%= stylesheet_link_tag "application" %>
<header>
<div id="headercontainer">
..
you should use a yield statement in your application.html.erb in the head element as such:
<head>
<%= yield :head %>
</head>
then in your view, you would use a content_for tag:
<% content_for :head do %>
<%= stylesheet_link_tag "dashboard" %>
<% end %>
also read the rails docs on nested layouts. it'll teach you how to get fancy with this paradigm
Create a separate layout for your dashboard, with specific stylesheets.
If you want customized stylesheet for one of your controllers (and all of its actions), it's quite easy to do. Create a layout with matching name, that is, for your users_controller, template name should be users.html.erb.
Also you can specify any layout for controller.
class UsersController < ApplicationController
layout 'some_layout'
end
If you want custom stylesheet only for some actions, specify corresponding layout in call to render.
def dashboard
# some logic here
render :layout => 'some_layout'
end
I would suggest you define a second layout for dashboard (app/views/layouts/dashboard.html.erb). From that layout link to the dashboard stylesheets. Then use that layout from your dashboard views or controllers.
Oh just use some if conditions to link different stylesheets in the same layout:
<%= stylesheet_link_tag(dashboard_views? ? "dashboard" : "application") %>
Just implement dashboard_views? helper method to return true or false based on your views.
Related
How would I display different versions of headers in Rails in my application.html.erb view? For instance, for my landing page, I have a pretty big header that includes a callout and some sign-up and sign-in buttons. For other static pages, I have a normal small header with a logo and a few links to the right. And for my dashboard when users log in, it will look a little different as well.
How can I display certain headers in an if/else statement where Rails will output the different versions of headers based on the current url or view in my application.html.erb?
To answer your question with an example this is what you may want to do.
Rails has a provision to use nested view templates using the content_for and yield tags.
Do the following thing to achieve what you want -
In app/views/layouts/application.html.erb - Add a ternary expression which acts as a if else. While rendering the views rails will look for a <% content_for :custom_header do %> in the view templates. If it doesn't find that it will render the partial app/views/layouts/_default_header.html.erb instead.
<html>
<head>
<title><%= #page_title or "Page Title" %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<!-- Code to conditionally load a header -->
<%= content_for?(:custom_header) ? yield(:custom_header) : render :partial => "layouts/default_header" %></div>
<!-- For the body -->
<%= yield %>
</body>
</html>
Now since you say that most pages will have a static small header, save the html code for that in a partial under app/views/layouts/_default_header.html.erb.
In you landing page (for example the the view app/views/welcome/index.html.erb) you can use the content_for tag with the identifier :custom_header that we have used in the application.html.erb
Add the following in your landing page.
<% content_for :custom_header do %>
<div class="custom-header">
<!-- Your complex header with sign in and signup links... -->
</div>
<% end %>
The ternary operator in the application.html.erb will pick up this content from the content_for tag in the landing page and insert the content in place of the yield(:custom_header).
Hope this helps.
It sounds like you may want to use a nested layout.
I'm trying to use Rails with Angularjs. Angular will do all the client side work while Rails controllers suppose to handle requests to list and modify information (in Database).
I have a simple template in views/layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>SomeApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= stylesheet_link_tag "vendors", :media => "all" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="wrapper" ng-app="app" ng-controller="AppCtrl">
<div ng-controller="SomeApprCtrl">
<%= render 'header' %>
<div ng-view=""></div>
<%= render 'footer' %>
</div>
</div><!-- End on wrapper -->
<%= javascript_include_tag "application" %>
</body>
</html>
This template does everything i need to start Angular. But i need to put something in Rails routes. I need to define controller and action. Say something like this:
root :to => 'main#index'
And this mean that I need to define controller Main with only one empty method index and totally empty template file in views/main/index.html. How can i avoid appearance of this empty useless files?
I was thinking of using root :to => 'application#index' and define empty index method in ApplicationController but since this controller is basic for inheritance to all other controllers i don't want them to have any crap. Also this approach does not solves problem with empty index.html template
While it is empty the controller is actually handling a lot by convention. It automatically displays the index.html.erb view, and since the layout hasn't been specified it wraps it in the application.html.erb layout.
So while the file may be sparse there's a lot going on behind the scenes.
My project is to make a website with html5 games, in Rails.
I have a controller called games_controller, and I would like to match /games/name-of-my-game with each html5 games. I created a route, matched with a games_controller function, but here is my problem:
I want everything to be "plug-and-play" for my html5 games, I mean develop them out-of-Rails, then just copy/paste it inside my app and everything works fine (references to images,.js files and all). But I also want my game's view to be inside my application layout. Here is what I tried :
Put it in /public directory, and in my games_controller's show method do render "/public/path_to_my_game". This breaks all references like <script type="text/javascript" src="some_script.js"></script>, cause the url isn't */path_to_my_game* but /games/name-of-my-game.
Put it in /public directory, but with a redirect_to in my show method instead render. The references are not broken, but the layout is gone (I may be wrong, but layouts are stuck with controllers, not static files).
Put it in app/views/games but this seemed ugly so I quickly stopped :).
Does anyone have an idea ?
If you have something like
# config/routes.rb
match '/games/:game' => 'games_controller#show'
# app/controllers/games_controller.rb
def show
#game = params[:game]
end
All you really have to do in your layout file is
<%= javascript_include_tag #game %>
This means you will have a app/assets/javascripts/<game>.js file for each game you have. It will be responsible for booting each game.
Obviously, you will want to take precaution to validate that params[:game] is a valid game.
Alternatively, you could create a content section in your layout file
<html>
<head>
<%= yield :game %>
</head>
<!-- rest of layout ... -->
Then serve the appropriate view with your GamesController
# app/controllers/games_controller.rb
def show
#game = params[:game]
render "games/#{#game}"
rescue ActionView::MissingTemplate
redirect_to root_path, :alert => "invalid game id"
end
Then in your game views, you will require all necessary files for each game. For example, a user visits /games/pacman
# app/views/games/pacman.html.erb
<% content_for :game do %>
<%= javascript_include_tag 'pacman/foobar' %>
<%= javascript_include_tag 'pacman/start' %>
<%= stylesheet_link_tag 'pacman/styles' %>
<% end %>
Another example, user visits /games/time_pilot
# app/views/games/pacman.html.erb
<% content_for :game do %>
<%= javascript_include_tag 'time_pilot/init' %>
<%= stylesheet_link_tag 'time_pilot/base' %>
<%= stylesheet_link_tag 'time_pilot/retro' %>
<% end %>
Because I'm sure each game is going to have a different amount of dependencies and they will all be initialized differently, you can build the view for each game accordingly and serve up whatever assets are relevant to each game.
Fore more help with with layouts and rendering, see the rails guide: Layouts and Rendering in Rails
Is it possible to manipulate the placeholders so that I can not only set their content, but also add/remove content in a particular order? For example:
layouts/base.html.erb (a base layout meant to be extended):
<!DOCTYPE html>
<html>
<head>
<title><%= yield :title %></title>
<%= yield :stylesheets %>
<%= yield :javascripts %>
<%= yield :csrf %>
</head>
<body>
<div class='container-fluid'>
<%= yield :header %>
<%= content_for?(:content) ? yield(:content) : yield %>
<%= yield :footer %>
</div>
</body>
</html>
layouts/application.html.erb (this is the layout I will be using for the most part of my app, it inherits from the base layout):
<% content_for :stylesheets do %>
<%= stylesheet_link_tag "application", :media => "all" %>
<% end %>
<% content_for :javascripts do %>
<%= javascript_include_tag "application" %>
<% end %>
<% content_for :csrf do %>
<%= csrf_meta_tags %>
<% end %>
<%= render :template => 'layouts/base' %>
Now I want a layout for a specific controller, which may need to add more javascript links, or maybe completely remove them. Let's say I want to add only one file after the other javascripts. So far I got this:
layouts/some_controller.html.erb (this is a layout for a particular controller, it should inherit from the application layout):
<% content_for :javascripts do %>
<script src="/assets/some_javascript_that_depends_on_jquery.js" type="text/javascript"></script>
<% end %>
<%= render :template => 'layouts/application' %>
This won't work, because it will place some_javascript_that_depends_on_jquery.js at the beginning of the :javascripts placeholder, and I need it at the end because it depends on jquery.
It would suck to have to extend the base layout directly, and keep track of any change made to the application layout to apply it to the controller-specific layout too.
What would be the recommended way to deal with this situation?
In application.html.erb, Keep the contents of content_for :javascripts in a partial
Here your partial will have
<%= javascript_include_tag "application" %>
Then, call the same partial in addition with other javascripts in other layout.
Another way,
You can call one helper which will have a hash like this:
js_files = {"application_controller" => ["js_file_1"], "some_controller" => ["js_file_1","js_file_2"]}
Now, fetch the js files and construct the javascript include tag in run time based on controller in your content for.
Hope this will be more flexible.
Sorry for not formatting.
Always keep one js file per controller.
<%= javascript_include_tag params[:controller] %>
lets take example of users controller then there will be users.js.coffee file.
If you want to have multiple js files for users controller then you can require those files inside users.js.coffee file
vi users.js.coffee
//= require 'a'
//= require 'b'
/* my extra js code will go here */
This can't be done as in other frameworks where you just extend layouts and then modify the inherited blocks at will.
Rails sort of forces you to keep it simple.
Sorry for the slightly noobish question, as I am writing my first rails app.
I get the idea of the layout view, but if you are using them, is there any way to include a view specific js or css file? For example, I have layouts/products.html.erb, and for products/edit.html.erb I want products_edit.css, but I don't want that css for all product views, what is the best practice to accomplish that?
If you have a generic edit.css file, I would suggest an if in your layout
<%= stylesheet_link_tag 'edit' if params[:action] == 'edit' %>
Otherwise you can use content_for with a yield to add additional tags into the head.
layout.html.erb
<head>
...
<%= yield(:header) if #content_for_header %>
</head>
products/edit.html.erb
<% content_for :header do -%>
<%= stylesheet_link_tag 'edit_product' %>
<% end -%>
You can add a stylesheet tag inside the head tag of the layout by doing something like this:
layouts/products.html.erb:
<head>
...
<%= yield :css %>
...
</head>
products/edit.html.erb
<% content_for :css do
stylesheet_link_tag 'products_edit'
end %>
You can't if your </head> tag is in your layout.
You may want a different layout for that controller action. Like this on the render:
render :action => "index", :layout => "some_other_layout
Also you can set a different default layout for a whole controller with this line in the controller class:
layout "some_other_layout"
Check the API docs, there's some complex things you can do with conditionals on that layout method.