Controller instance methods available in rhtml?


#1

In rhtml, you have access to controller. I am trying to set an instance
var
in my controller, then retrieve it in my form display. I know I could
do
this through one of the hashes I have access to, but I’m trying to
understand why I can’t do it this way.

No matter what, when I try to output from my rhtml either
<%= controller.whynot || ‘Unknown’ %> or <%= controller.othermessage ||
‘Unknown’ %> I don’t get the values I’m setting. Here’s some of the
ways I
try:

class ApplicationController < ActionController::Base

attr_accessor :othermessage

def whynot=(msg)
@msg = msg
end

def whynot
@msg
end

class AgentsController < ApplicationController

def show
othermessage = ‘show set’
whynot=‘jim’
@agent = Agent.find(params[:id])
end


#2

Hi Jim.

I think the controller makes it’s instance variables avaiIable to the
view. At least you can use it as though it does. Instead of using
<%= controller.whynot …-%>

you would just use
<%= @msg | “Whatever” %>

Alternatively if you want to have these methods available in your
views then use

class ApplicationController < ActionController::Base

attr_accessor :othermessage

def whynot=(msg)
@msg = msg
end

def whynot
@msg
end

helper_method :whynot

…snip …

That way you can use the whynot method directly in your views.

eg
<%= whynot || “Whatever” %>

Hope this helps ( & is strictly correct.)