# Setting a variable for view

**URL:** <https://www.ruby-forum.com/t/setting-a-variable-for-view/73205>\
**Category:** Rails\
**Created:** [October 22, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/setting-a-variable-for-view/73205 "2006-10-22T21:53:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 22, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/setting-a-variable-for-view/73205/1 "2006-10-22T21:53:56Z")

</div>

I’d like to set a value somewhere to let the view know if it is in “add  
new” mode or “Update” mode. Depending on the mode, one line of text in  
the view is to be presented differently (e.g. \<% if updateMode == true  
%\>).

This brings up a couple of questions:  
Where is the best place to store that value?  
What is the proper way to set the value in the controller and to  
retrieve it in the view?

Thanks in advance  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 22, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/setting-a-variable-for-view/73205/2 "2006-10-22T22:03:01Z")

</div>

Hi –

On Sun, 22 Oct 2006, Michael S. wrote:

> I’d like to set a value somewhere to let the view know if it is in “add  
> new” mode or “Update” mode. Depending on the mode, one line of text in  
> the view is to be presented differently (e.g. \<% if updateMode == true  
> %\>).
> 
> This brings up a couple of questions:  
> Where is the best place to store that value?  
> What is the proper way to set the value in the controller and to  
> retrieve it in the view?

Instance variables set in the controller are visible in the view. So  
you would want to do:

# Controller:

def my\_action  
@update\_mode = true  
…  
end

# View:

\<% if @update\_mode %\>  
…  
\<% end %\>

(Note that you don’t need “== true”.)

Mind you, if you find yourself doing this a lot, it’s probably a sign  
that you should reorganize your actions and views, perhaps by  
splitting off an update sequence. I’ve run into situations like that  
with @admin\_session and other such flags: I realize eventually that  
I’m really trying to squeeze two separate use cases into one view,  
where it might be better, and scale better, to have a separate suite  
of admin tools.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 23, 2006, 12:31am UTC](https://www.ruby-forum.com/t/setting-a-variable-for-view/73205/3 "2006-10-23T00:31:35Z")

</div>

Hi Michael,

Michael S. wrote:

> I’d like to set a value somewhere to let the view  
> know if it is in “add new” mode or “Update” mode.  
> Depending on the mode, one line of text in the view  
> is to be presented differently (e.g. \<% if updateMode == true  
> %\>).

The simplest approach might be to set an instance variable (e.g.,  
@updateMode) in your controller and then do a check (e.g.,\<% if  
!@updateMode.nil? %\>) in the view to figure out where you are.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 23, 2006, 1:23am UTC](https://www.ruby-forum.com/t/setting-a-variable-for-view/73205/4 "2006-10-23T01:23:41Z")

</div>

If this specific issue as what you’re addressing, how about:

if @my\_model\_object.new\_record?

# do new code

else

# do edit code

end

of if you are just changing the button names, use something like:

\<%= submit\_button(my\_model\_object.new\_record? ? ‘add new’ : ‘update  
now’) %\>

Yes?
