DRY Rails Master Templates? - ruby-on-rails

Is there a simple way to define a master template for my whole rails application? If not, what's the best way to reuse my templates so that I'm not copy and pasting the same template into a bunch of layout files?

You can name it application.html.erb and Rails will use it for the whole app.
More info at rails guides.

Create an application.html.erb file in the layout folder of the views. It will be called if the controller has no template, so you might need to remove them.
You can also define a template for a specific controller going
class FaqentriesController < ApplicationController
layout "admin"
[..]

/app/views/layouts/whatever.rhtml (or whichever extension your prefer to work with):
<html>
...
<%= yield %>
...
</html>
/app/controllers/ApplicationController.rb:
layout "whatever"
(Edit: I can't remember off the top of my head whether calling the layout application.rhtml (or whatever) automatically makes it the default layout for any controller lacking specification or whether this bit of magic is incorporated into the default ApplicationController when you generate scaffolding, using the above syntax.)

Related

ApplicationController layout is being overriden by inherited classes? Ruby on Rails 2

I am developing a Rails 2 application and only have the need for one layout template for my multiple controllers so I created one application.html.erb to be used and put
layout 'application'
into my ApplicationController (which all the controllers are definitely inheriting from) but the controllers still render their default layouts upon running the application.
I have seen from this page that inserting the layout method into my ApplicationController is all that is needed to make a default layout, and it seems to be overridden by the inherited controllers. Any ideas why this is happening?
If there are other layout view files they will be used over the default. You need to either remove the other layout files (they will have the same name as a controller), or define layout 'application' in every controller that you want to use it.

HAML app layout not being picked up?

I'm working on my 1st Ruby on Rails app. I have set up HAML as my formatter and it is rendering views fine.
However, I have a views/layouts/application.html.haml file for the basic layout, but it is not being picked up at all. All I see on the page is may view's HTML.
What could make this fail to be picked up?
Make sure your controller extends ApplicationController and not ActionController::Base.
class SomeController < ApplicationController
can you assert that application.html.erb has been removed from the app/views/layouts directory?

Create a "main layout" view in Rails?

Ok so I have a small project with two different scaffoldings I've made.In the layouts directory therefor there is two different layout.html.erb files.
My question is how to condense this into just one main layout file that the two scaffolded views share.
Basically my purpose for doing this is so that I only have to have my navigation bar and header and other such things all in one place.
if you name the layout file application.html.erb, then it will be the default layout file. If you specify a layout file by the same name of your controller, that will override the default layout.
From Rails Guides:
To find the current layout, Rails
first looks for a file in
app/views/layouts with the same base
name as the controller. For example,
rendering actions from the
PhotosController class will use
app/views/layouts/photos.html.erb (or
app/views/layouts/photos.builder). If
there is no such controller-specific
layout, Rails will use
app/views/layouts/application.html.erb
or
app/views/layouts/application.builder.
If there is no .erb layout, Rails will
use a .builder layout if one exists.
Rails also provides several ways to
more precisely assign specific layouts
to individual controllers and actions.
source: http://guides.rails.info/layouts_and_rendering.html
EDIT:
I should add that you can specify any layout to be your default in the Application Controller:
class ApplicationController < ActionController::Base
layout 'some_layout_name'
end
And that will override name matching that rails does automatically.
I hope this helps!
You can have an application.html.erb in your layouts directory that will be shared by all of the views

Ruby on Rails: How do I specify a non-relative path to a layout when calling render?

(Rails version 2.3.2)
By default the :layout parameter for render takes a relative path and adds this to the default layout directory ("app/views/layout").
Eg:
render :file => '../resources/website/home_page.html.erb', :layout => '../../../../resources/website/layout'
"If no directory is specified for the template name, the template will by default be looked for in app/views/layouts/. Otherwise, it will be looked up relative to the template root."
-http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html
However, the above only works in development mode, and breaks in production, failing to find the template. Exception: ActionView::MissingTemplate
Either way, I would rather specify the direct path to a layout file.
(The idea is to keep the specified layout file separate from the main project views, in a plugin-like way.)
Is this possible?
I could temporarily (instance only) override the method "default_layout" in ActionController::Layout? (But im not sure how?)
Thanks for reading.
If you need to resolve layout per request, try:
class ApplicationController < ActionController::Base
layout :resolve_layout
# some definitions
protected
def resolve_layout
# some logic depending on current request
path_to_layout = RAILS_ROOT + "/path/to/layout"
return path_to_layout
end
end
I hope, that is what you need.
Probably the only good way to do this would be to make a constant in your environment.rb with the path for whatever box you're on. So something like
LAYOUT_PATH = '/var/www/templates'
The other option would be to keep the templates in the correct directory but use an svn external or the equivalent in your SCM of choice to keep that template directory up to date with all the other sites that use the same templates.
You can probably add to the controller view paths (see here) to allow your app to pick up templates from different directories. This could potentially also cure your other weird template paths.
If you want absolute paths, use RAILS_ROOT, as suggested here. If you want to share views from a plugin, you may also want to check out the rails-engines plugin.
But also remember that Rails (kind of intentionally) makes doing strange things hard. If you don't have a really strong reason to do otherwise, you'll enjoy a smoother ride by sticking to the defaults.

Using Liquid as a Ruby on Rails layout

I want to create a Ruby on Rails layout and it should be in Liquid format.
Basically what I'm trying to do is to let the users to create their own layouts and save them in the database.
I tried to use
<%= Liquid::Template.parse(<code from database>).render %> in my layout.erb file but there I can't use the 'yield' command (since this is a layout I should have to have a way of rendering pages.)
But when I use 'layout.liquid' with {{ content_for_layout }} is will work find BUT, cannot load details from the database (I mean the HTML code..)
I hope I made myself clear :D )
Take a look at this Ruby on Rails plug-in:
http://github.com/akitaonrails/dynamic_liquid_templates
Next we have to find a way to intercept the default Ruby on Rails behaviour for your controller.
class MyAwesomeController
layout :get_my_db_layout
....
protected
def get_my_db_layout
'as_if_by_a_miracle.liquid' # add your db finder here
end
end
Then, overwrite LocalFileSystem#read_template_file with your own class / method, to get the template from the database. LocalFileSystem#read_template_file is a Liquid class.
I hope, that this idea is helpful.
please read Tobis article on
https://github.com/shopify/liquid/wiki/getting-liquid-to-work-in-rails
or look at this screencast
http://railscasts.com/episodes/118-liquid

Resources