Template/variable confusion

Hi there,
I’m having some trouble working out where/when I can define template
variables.

I have a standard.rhtml layout template which is used across the
application as the main xhtml wrapper.
Inside there is some code like this:

<div id="<%= @layout_style %>">
<%= @content_for_layout %>
</div>

Now content_for_layout always works fine, but @layout_style seems to
only work if I set it either within a method in a specific controller
thus:

@layout_style = 'content'

or when I define it at the top of the appropriate @content_for_layout
template thus:

<% @layout_style = 'index' %>

I was hoping that I would be able to set variables such as this globally
within the application.rb controller.
Or, am I just going about this in completely the wrong way :slight_smile: ?

I am also having trouble writing (what I think are called) overload
methods within the model. I have a TIME column named running_time in my
Film model and I only ever want it to look like this e.g.: ‘5:32’ rather
than the fully rendered datetime that is returned by default. So I
define a method in the model to override the default:

def running_time
  # strftime code here
end

but whenever I try and reference the value of running_time from withing
the method I get a stack overload.
What would be the best way to do what I initially though should be done
like this:

self.running_time.strftime("%M:%S")

?

It’s taking me a while to get the idioms and the heuristics for RoR…
Thanks in advance for any help available!

dorian


I do things for love or money

+44 (0)7941 219 501

aim:oulalipo | yahoo:tachekent

I would create multiple layouts (put them in
app/views/layouts/???.rhtml), and set them in the controller:

class TestController < ApplicationController
layout ‘content’
end

would use content.rhtml.

Maybe:

def running_time
super.strftime("%M:%S")
end

or:

def running_time
read_attribute(:running_time).strftime("%M:%S")
end

Jules

Dorian M. wrote:

but whenever I try and reference the value of running_time from withing
the method I get a stack overload.
What would be the best way to do what I initially though should be done
like this:

self.running_time.strftime("%M:%S")

I think you need to create an alias to the original method.

alias :old_running_time :running_time

def running_time
self.send(:old_running_time).strftime("%M:%S")
end

That way it won’t try to keep calling itself.

_Kevin

Thanks for that,

I would create multiple layouts (put them in
app/views/layouts/???.rhtml), and set them in the controller:

class TestController < ApplicationController
layout ‘content’
end

would use content.rhtml.

I understand that bit, but the layout is so similar that it seems a bit
unnecessary and against the whole DRY principle to repeat it.
I know I can use partials but I need foolproof access to the main
display template.

end
actually I sorted it by using:

self[:running_time].strftime("%M:%S")

is that bad?

Jules

thanks again

dorian


I do things for love or money

+44 (0)7941 219 501

aim:oulalipo | yahoo:tachekent

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs