Rails 3, HowTo - Pass a Setting to the Application.html.erb

Most of my views will required a wrapper of padding 10px, but a few
will not…

I was thinking of doing something like this in the view controller:

 respond_to do |format|
   format.html { render :layout => true, :padding => 'false' }

And then in the application.html.erb have an IF to not add a padding
class if :padding is false… But the above idea doesn’t work, the
variable padding is not being passed.

Any ideas? Or cleaner/smart solutions? thxs

Try doing this in your controller:
@padding = false

Then in your view you can test it like this:
<% if @padding %>
//do stuff here
<% end %>

I found it pretty helpful to work through this:

Luke

In my opinion you should have 2 layouts.

  • application,
  • padding.

The `padding’ layout should be nested inside application and
(according to its name) add some padding :-). Look here about how to
do it in rails 2 and 3:
https://rails.lighthouseapp.com/projects/8994/tickets/5305-rails3-rc-named-yield-should-return-nil-when-content_for-with-that-name-was-not-called

With that you could setup layout per every action.

layout :application
layout :padding, :only => [:new].

The second way of doing this:

Add one more stylesheet in every view that needs padding:

#application.html.erb layout

<%= yield :head %>

#new.html.erb
content_for(:head) do
stylesheet_link_tag ‘padding’
end

You could abstract it into helper method and use as simple as:
#new.html.erb
<%= padding() %>

Third way is to create helper that would be used this way:

<%= padding do %>
create your content here
<% end %>

I wouldn’t bother controller with such a minor change in view layer so
I prefere keeping padding in views instead changing layout on the
controller side.

Robert Pankowecki

Or you could add something like

… your stuff

and set a conditional css class which can handle the padding. Set the
class with content_for()

On Oct 9, 10:32 am, “Robert Pankowecki (rupert)”