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?
Related
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.
On the rails 3.2 release notes page (http://guides.rubyonrails.org/3_2_release_notes.html), it says:
Deprecated implied layout lookup in controllers whose parent had a explicit layout set
But I tried the following in my rails 3.2.6 app:
class ApplicationController < ActionController::Base
protect_from_forgery
layout "application_main"
end
class HomeController < ApplicationController
def index
#slideshow_pics = Event.get_intro_slide_photos
end
end
with layouts application_main.html.haml and home.html.haml defined and when i go to the home#index page, I get the home.html.haml layout rendered instead of the other.
This seems to go against the deprecation so I was wondering, did one of the releases since 3.2.6 regress the deprecation?
To be even more clear than Dave above, "Deprecated" means: Watch out, we're going to remove this functionality in the future! We're throwing this warning so you pay attention and you better change this soon as we are change how things work!
It doesn't mean that the functionality has already been changed.
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
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
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.)