Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
render :file => "#{RAILS_ROOT}/public/layouts/mylayout" cached
5 Posts
render :file => "#{RAILS_ROOT}/public/layouts/mylayout" cached

Hi, yes, using render: file the controller caching the html.erb templates that are in the public path, including in development environment, regardless of layout. I don't know a direct option to change this particular behavior (if anyone knows it would be interesting to share it), but an alternative that works to force reloading the file template is to have a auxiliar template for rendering the file, as follows:

YourController class < ApplicationController

def  my_action
  ....
  render :file =>  "#{RAILS_ROOT}/public/files/render_file.html.erb", :layout => false
  ...
end

end

...
{RAILS_ROOT}/public/files/render_file.html.erb

... my_file.html.erb is the file that is updated

In this way the file is not cached and you will not need to restart the server when you edit the file while you are developing

Person
Forum Rating: 100
Sign In To Rate Post

class YouController < ApplicationController

def you_action

render :file => "#{RAILS_ROOT}/public/files/myfile.html", :layout => false

end

end

Dear Fernando, Thanks for your reply. Actually I want to render no layout :). I need to render a file(located in public/files/myfile.html.erb) with no layout. I have a special reason to do this.

Person
Forum Rating: 100
Sign In To Rate Post

Hi Abdul,

Is there a special reason for you to store layouts in the public folder? The default path for layouts is {RAILS_ROOT\app\view\layouts}

eg RAILS_ROOT\app\view\layouts\my_layout.html.erb

If you use the default path in the development environment you will not have to restart the server every time you edit the file. Rails is configured to implement file caching to improve performance.

Another detail is that the standard way to render a layout in a controller is:

MyController class

 # ...

end

If you name your layout as application.html.erb and put it in default directory (RAILS_ROOT\app\view\layouts\application.html.erb) then the layout is applied to all controllers that have no associated layout (ie not having a layout with the same name as the controller).

you can see more details in the following link: http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html

regards

Person
Forum Rating: 100
Sign In To Rate Post

render :file => "#{RAILS_ROOT}/public/layouts/mylayout.html.erb". Everytime when i edit mylayout.html.erb file, I have to restart my app server otherwise Rails caches.

Can anyone please tips me regarding this caching issue so that i have not to restart my server?

Person
Forum Rating: 100
Sign In To Rate Post
5 Posts
Login to add your message