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
Related
I am using rails 4 from Suspenders and need to include content from a middleman-blog rack app from a subfolder in a view. In my app, I have the blog content in:
rails root > my_blog > source > index.html.erb.
I have created the view as:
rails root > app > views > welcomes > index.html.haml
The code in my view is:
%h1 Welcome
= render "my_blog/source/index.html.erb"
But when I access the page I get a Missing Partials error and the message says it only looked in the views folder.
How can I render content from a folder outside views?
Partial
The problem is Rails is trying to call a partial, which is not what you're trying to call. A partial should have its name preceded with an _underscore, indicating to Rails that it's a partial, hence why you're receiving your error
The reason this is important is because although you're just calling render, you will actually call the partial too
--
Convention
One of the issues you have is that you'll be going against convention in several ways:
MVC dictates the "view" will be loaded per request (so Rails will expect it to just be present whenever you use it)
The "partials" functionality of your system needs to add to the views you're showing the users. This means you have to be sure you
have a view already showing on screen
This means you need to be certain if you're meant to be using a partial or other element in this part of your app. From the looks of it, whilst you may be doing something right, you need to be sure you're able to load the partial correctly:
<%= render "your/partial/path/_partial_name.html.erb %>
--
View Path
Further to your view path issue, although I've never encountered this issue directly, there is a function called append_view_path, which allows you to add another "path" to look at for your app:
#app/controllers/welcome_controller.rb
Class WelcomeController < ApplicationController
append_view_path(File.join(RAILS_ROOT, "app/themes/#{#current_theme}"))
end
Try this is in your view file:
%h1 Welcome
= render :partial => "my_blog/source/index"
Note: You must have _index.html.erb partial file in the above specified path.
Also, try to change the name from _index.html.erb to _someothername.html.erb, because index.html.erb is usualy a view file for index action. You can avoid the unwanted confusions.
This may help you..!
Thanks!!
Added Lines (Edited):
Please change your file name from
rails root > my_blog > source > index.html.erb.
to
rails root > my_blog > source > _index.html.erb.
Have you tried the full path?
= render "#{Rails.root}/my_blog/source/index.html.haml"
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.
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?
(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.
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.)