Forum: Ruby on Rails controller instance methods available in rhtml?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B7ae1a166a9f42a2cb7e87fda8f811e2?d=identicon&s=25 Jim mack (Guest)
on 2006-06-02 08:20
(Received via mailing list)
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-06-02 08:59
(Received via mailing list)
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.)
This topic is locked and can not be replied to.