Why does Rails render RJS templates within a layout? - ruby-on-rails

I need to do this for a controller which uses the active_scaffold gem. We have a controller that looked something like this:
class Admin::UsersController < ApplicationController
layout 'admin'
active_scaffold :users do |config|
config.search.columns = [:first_name, :last_name]
end
end
That worked great when we were on Rails 2.3.10, but we're upgrading to Rails 3.0.10. As part of the upgrade, I had to upgrade active_scaffold (currently installed from the rails-3.0 branch of git://github.com/activescaffold/active_scaffold) to be compatible. One problem we were having is that searching the table wasn't working. I would see in my log files:
Rendered <snip>/gems/active_scaffold-25b3d724f35b/frontends/default/views/list.js.rjs within layouts/admin (923.5ms)
Notice that it's rendering the RJS template with the layout specified in the controller. That seems like an unreasonable default to me. Shouldn't RJS templates render without a layout by default? Anyway, I fixed it as such:
class Admin::UsersController < ApplicationController
layout :admin_layout
private
def admin_layout
respond_to do |format|
format.js { false }
format.html { 'admin' }
end
end
end
That fixes the issues with search and pagination. (The RJS template is now rendered without a layout, so the browser can execute the resulting Javascript). I guess my question is, why do I have to tell Rails that it shouldn't render RJS templates with layouts? And is there a better solution? This feels like too much of a hack to me (the bad kind of hack---the kind of hack that will break in the future).

Okay, I figured it out. #numbers1311407's comment under my question led me to check the name of the layout template. It was layouts/admin.haml. With Rails 2, that layout was only rendering for HTML requests, but with Rails 3 it applies to all requests (because it doesn't specify a format type). I renamed it to layouts/admin.html.haml and it works with a simple layout 'admin' in my controller (as opposed to the hack that I had come up with in my question).
So the answer to the question, "Why does Rails render RJS templates within a layout?" is that the layout file was named such that it applies to all formats.

Answer your quastions:
1. There is no magic that Rails renderers layout in for JS format. That's bacause it is default to Rails to render layout with any template unless you explicitly tell to avoid it. You can just look into Rails sources in file: actionpack/lib/action_controller/metal/renderers.rb to see :js renderer.
2.Try to use:
respond_to do |format|
format.js { render *your_any_options*, layout: false }
end

Related

Is there a way to render a different template based on current user without using conditionals

I have two different layouts, one is completely custom and the other is bootstrap. For admins we want to render a bootstrap view and for non-admins we render it normally. For the most part this is pretty straight forward because admins and users don't share many views -- but there are some.
My original idea involved overriding render so that it would check if there's a bootstrap version of a file. So for example there would be _user.html.erb and _user.bootstrap.html.erb which would have bootstrap specific templating.
I'd like to not modify any controllers so ideally, something like render 'form' would behave smartly and check if there's an _form.bootstrap.html.erb, and if there isn't it would fallback to _form.html.erb
First attempt
My first attempt looked something like this
# I don't think this is the actual method signature of render
def render(options=nil, extra_options, &block)
# if it should render bootstrap and options is a string and there exists a bootstrap version
# set it up to render the bootstrap view
super(options, extra_options, &b)
end
Current attempt
I'm thinking about registering a template that basically checks if a file exists and then uses erb. I haven't made any progress towards this yet.
I figured it out. This is how I did it:
This is set in the application controller, with a before_filter :render_bootstrap
def render_bootstrap
return unless bootstrap?
new_action = "#{self.action_name}.bootstrap"
has_bs_view = template_exists?(new_action,params[:controller],false) || template_exists?(new_action,params[:controller], true)
if has_bs_view
self.action_name = new_action
end
end
I decided to extend this even further so that inside of a view like show.bootstrap.html.erb you can still use render "form" without doing render "form.bootstrap". This was done by overwriting the rails render helper.

Rails 3 rendering template missing without file ending

I have an action in my controller as below:
def show
#post = Post.find_by_setitle(params[:setitle])
if !#post
render 'error_pages/404'
return
end
respond_to do |format|
format.html
end
end
If the render error_pages/404 I get a template missing. Switching it to render error_pages/404.haml.html works fine.
Why is this?
N.B. There is no actual error_pages controller or model. Just a convenient place to keep them.
Edit: I'm using mongoid and hence don't have access to ActiveRecord. Controller base can't be looking for a particular ActiveRecord exception?
From the documentation
The render method can also use a view that’s entirely outside of your application (perhaps you’re sharing views between two Rails applications):
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the :file option (which was required on Rails 2.2 and earlier):
You need either to pass the :file option, or to start the location string with a slash. Alternatively, you could use the Rails functionality to rescue from errors, and recover from ActiveRecord::RecordNotFound with a 404. See this post for details.
You should probably use render :template => 'error_pages/404'.
I think Rails is looking for a partial called _404.
Try it out 1:
render 'error_pages/404' (and name the file _404.html.erb)
Try it out 2:
render :template => 'error_pages/404' (and name the file 404.html.erb i.e. no leading underscore)

Ruby on Rails - How to manage layouts in admin interface?

I have a very simple site setup using awesome_nested_set and a single table called Pages.
I would like the ability to select different layouts in the admin when creating and updating Pages. What I envisioned is a drop down on the Pages form that allowed me to select a layout/template.
The only thing I know about layouts is you are required to add them to /views/layouts/ and specify the layout at the top of the controller. I need a way to manage layouts on a per Page basis inside the app itself.
Is that even possible? If so, can you explain on a high level how that might be done so I can have a starting point?
edit
Something like this:
You can easily change the layout at render by supplying the :layout key like so:
def some_action
#... stuff
render "some_action", :layout => "custom_layout"
end
You can also set layout to a symbol in the controller definition, and the controller will run the associated method to decide what layout to choose
class UsersController < ApplicationController
layout :decide_layout
private
def decide_layout
some_boolean ? "layout1" : "layout2"
end
end
You can also replace the symbol with a proc if you don't want the method located away from the usage. Finally, you can also call #layout in an action itself.
Assuming you have files in views/layouts called something like one_column.html.erb, two_column.html.erb, etc., and an attribute called layout on you page model, you could just do:
def show
#page = Page.find(params[:id])
render :action => "show", :layout => #page.layout
end
Is that what you're looking for?

Asp.net MasterPage equivalent in Ruby on Rails, Trying to define a site wide layout

Asp.net WebForms and MVC has a concept of Masterpages which make it easy to define a one time layout for all the page of your site. In Rails I'm struggling to find an equivalent usage pattern or feature.
From what I've read it's really easy to define a layout in every action with:
layout: 'viewname'
Now that seemed pretty ceremonial to include in every controller so I added:
layout: 'application'
in the base ApplicationController.
So far this is working ok unless I have a more specific layout in the view pages. Is this common technique for getting a consistent style in your Rails application?
Imagine a simplified blog where we have a controller called PostsController which has two actions: index and show
The index action is called when the user hits http://yourwebsite.com/posts - this action displays all of the available blog posts.
The show action is called when a user gets a specific blog article - i.e. http://yourwebsite.com/posts/article-about-something-interesting
Let's say that we want the index page to have a two column layout and we want the show page for each blog-article to have a three column layout. To achieve this, we would simply define two separate layouts (in app/views/layouts folder) - we'll call the two column layout "application" and we'll call the three-column layout "alternate".
In order to get the index page to use the two-column layout and the show page to use the three-column layout, we could just do the following in our controller:
class PostsController < ApplicationController
def index
#posts = Post.all
render :layout => "application"
end
def show
#post = Post.find(params[:id])
render :layout => "alternate"
end
end
If we want all actions to use the same layout, we can just do this:
class PostsController < ApplicationController
layout "application"
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
end
Finally, if we do not specify which layout we want to use, then Rails will by default, render any layout which has the same name as the resource we are displaying. So in our example, where our resources are called "Posts", if we define a third layout called posts.html.erb (in app/views/layouts) then Rails will automatically use that layout when the user renders any of the actions in the PostsController - providing of course that we have not explicitly asked Rails to render another layout....
Hope it helps,
This PDF book excerpt from Rails for .Net Developers has a pretty good explanation of Rails layouts, along with a comparison to ASP.Net MasterPages. Since it seems to work pretty well, it's probably used fairly often, at least by developers familiar with the master page concept.

How to use partial in views with different alias MIME?

Im using 2 different sets of views for 2 different user's roles.
Im using register_alias :
Mime::Type.register_alias "text/html", :basic
in the controller:
class SomeController < ApplicationController
def index
# …
respond_to do |format|
format.html # index.html.erb (advance)
format.basic # index.basic.erb
end
end
end
In some case I have to use the same code in both views, then I would use a Partial, but because of the MIME alias, I have to use 2 identical partials:
my_partial.html.erb and my_partial.basic.erb
I think there is a solution to DRY the code and use only a partial.
Do you have some solutions ?
thank you,
Alessandro
Old Answer:
I probably tried 50 different things until I figured out the right way of writing the partial once, but it was worth it because it's super simple:
Inside your index view, you normally do:
<%= render "my_partial" %>
This implicitly gets mapped to the partial corresponding to the Mime you requested, so it implies having two partial implementations. If you want a DRY partial, simply explicitly specify the format:
<%= render "my_partial.html" %>
As an added bonus of this observation, if your responds_to block of code is really just to switch based on the format and has no logic inside it, you can entirely remove that block of code and things still work implicitly.
Rails 3.2 update:
Rails has deprecated support for the above and support has been completely removed in the latest version of Rails. The following is the correct way as of Rails 3.2:
<%= render :partial => "my_partial", :formats => [:html] %>

Resources