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

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.

Related

Rails views inheritance

I have a rails application with an engine inside it.
Is it possible for the engine to inherit base application view?
I mean, if I have the #{Rails.root}/app/views path in the engine's view_paths, the views will be searched in:
/app/views/my_engine/controller_name/action
I need instead to put the views in:
/app/views/controller_name/action
It is easy to use an application wide layout using the layout() method inside the engine, but no luck using base views for each action.
Any suggestion?
Solved adding a prefix to the Action View lookup_context
lookup_context.prefixes << 'controller_name'

Why does the layout for rails project get ignored?

I've attached a picture of my project which I just created. Even though the application.html.erb is present my template ignores it and the page is rendered without the surrounding html inside the layout.
I have even tried to specify the layout using the layout option in my controller without success.
The project was generated with --skip-active-record flag because I'm using mongodb without an ORM. I don't think this has anything to do with it. Right??
Worked out the problem.
In my controller I had defined a new constructor which didn't call its super class like so:
def initialize
#default_report_days = 30
end
Once the controller was correctly initialised the layout started being picked up.
def initialize
super
#default_report_days = 30
end

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

DRY Rails Master Templates?

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.)

Resources