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
Related
Will a file in helper directory included in all controllers?. I didn't find any good explanation regarding this. I have 2 custom directories in my controller( like admin, for normal user). Do I have same directory structure at my helper?. Is Helper name same as controller name only for readability?
By default all helper files under app/helpers are included in all controllers. As such, it doesn't matter how you structure what's inside helpers folder. If you really want to enforce controller to only include matching helper then set config.action_controller.include_all_helpers in config to false.
See comment section for details: https://github.com/rails/rails/blob/b5db73076914e7103466bd76bec785cbcfe88875/actionpack/lib/action_controller/metal/helpers.rb
Helper is just a ruby module which is openly available for views and controllers. You should never keep your code in helper if you do not want it to expose to views.
If you want to use helper methods for all your controller and views. Then you can add methods to application helper and include it to application controller. However if you don't want to expose methods to views, then you can use rails concerns. create a methods inside it and include it inside different controllers.
No helper do not name the same name only for readability. you still need to include inside your same name controller to call functions if you want to use it inside controller. But you can still use inside views methods with same name.
I am aware that I can override an applications view from within an engine by simply creating the same file within the engine and removing it from the application (eg: 'users/show.html.erb').
However, what I want is to be able to extend the applications view, not override.
Lets say I have a yield inside 'users/show.html.erb' of the main application:
yield :foo
What I want is for the engine to specify the same file 'users/show.html.erb' and to have a content_for block
content_for :foo {}
Thereby, injecting some template data from the engines view, into the applications view.
Obviously, the above won't work as once it has found the template file in the application, it won't look for one in the engine.
Is there a way to make this work?
There is no way to extend views that way in Rails. You can however, accomplish this by using partials. In your engine, write a partial view file users/_show.html.erb and then render it in your app's view:
# app/views/users/show
# will look for a partial called "_show.html.erb" in the engine's and app's view paths.
render partial: 'show'
It's just as easy as your suggestion.
This gem tries to implement partial extension of views, but it works similarly to what I just described: https://github.com/amatsuda/motorhead#partially-extending-views-in-the-main-app
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.
Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.
Inside 'Soho', I have a application.html.erb for layout.
Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.
My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?
Thanks!
I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...
All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.
You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.
After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.
If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts
Here's an example:
class UsersController < ApplicationController
layout "users"
#...
end
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.)