# If / Else Form layout question

**URL:** <https://www.ruby-forum.com/t/if-else-form-layout-question/96632>\
**Category:** Rails\
**Created:** [June 30, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632 "2006-06-30T12:04:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/1 "2006-06-30T12:04:42Z")

</div>

Hello all,

I’m using the following \_form.rhtml layout for a number of pages,  
including edit record and new record (which each has it’s own definition  
in the controller).

I’d like to display a different author form field depending on which  
page they are using. I’d rather keep using \_form.rhtml as a layout  
template, instead of customizing each separate page.

How do I go about setting up the if statement? Is there such a thing as  
a ‘if action =\> new’ or ‘if action =\> edit’ statement?

Thanks.

Title  
\<%= text\_field 'article', 'title' %\>

Body  
\<%= text\_area 'article', 'body' %\>

Author  
\<%=

if ???

#if new record page, insert the logged in users name  
text\_field ‘article’, ‘author’, ‘value’ =\> @username.name,  
‘disabled’ =\> ‘disabled’

else

#if existing record to edit page, just display the name already in  
database.  
text\_field ‘article’, ‘author’

end

%\>

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/2 "2006-06-30T12:37:50Z")

</div>

> How do I go about setting up the if statement? Is there such a thing as  
> a ‘if action =\> new’ or ‘if action =\> edit’ statement?  
> The controller variable @action\_name should contain the current action.

Steve

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/3 "2006-06-30T14:21:44Z")

</div>

Stephen B. wrote:

> > How do I go about setting up the if statement? Is there such a thing as  
> > a ‘if action =\> new’ or ‘if action =\> edit’ statement?  
> > The controller variable @action\_name should contain the current action.
> 
> Steve

or params[:action] params[:controller] etc

jeff

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/4 "2006-06-30T15:09:01Z")

</div>

Hi Jon,

I really shouldn’t do email before my second cup. (Sorry about that  
Stephen. 😉 )

Jon H. wrote:

> I’d like to display a different author form field depending on which  
> page they are using. I’d rather keep using \_form.rhtml as a layout  
> template, instead of customizing each separate page.

If you surround your form field with an if statement, it will only be  
rendered if the statement succeeds.

\<% if @author\_name == ‘bob’ %\>  
\<%= text\_field\_tag(‘field\_for\_bob’) %\>  
\<% elsif @author\_name == ‘sam’ %\>  
\<%= text\_field\_tag(‘field\_for\_sam’) %\>  
\<% elsif @author\_name == ‘sally’ %\>  
\<%= text\_field\_tag(‘field\_for\_sam’) %\>  
\<% else %\>  
\<%= text\_field\_tag(‘standard\_field’) %\>  
\<% end %\>

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/5 "2006-06-30T15:58:55Z")

</div>

> I really shouldn’t do email before my second cup. (Sorry about that  
> Stephen. 😉 )  
> You may need another - my post was a response to the original question  
> :0D

Steve

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [June 30, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/if-else-form-layout-question/96632/6 "2006-06-30T14:53:36Z")

</div>

Hi Stephen,

Stephen B. wrote:

There are two parts to your question.

> How do I go about setting up the if statement?

This is the easy part.

\<% if @action\_name == ‘one\_thing’ %\>  
… do that stuff  
\<% else %\>  
… do other stuff  
\<% end %\>

> Is there such a thing as a ‘if action =\> new’ or  
> ‘if action =\> edit’ statement? The controller variable  
> @action\_name should contain the current action.

This is more difficult, not to do, but to understand what you’re trying  
to  
do. The page rendered by the view is sitting there on the visitor’s  
desktop  
waiting for that visitor to take some action. The visitor’s action  
(e.g.,  
clicking on a link or a button) can determine what message (i.e., what  
controller / action pair) is sent to the server. You can use an if-else  
to  
present different buttons, for example, depending on the value of some  
variable. That would look like…

\<% if @action\_name == ‘one\_thing’ %\>  
\<%= button\_to(:controller =\> ‘your\_first\_choice’, :action  
=\>‘something’)  
%\>  
\<% else %\>  
\<%= button\_to(:controller =\> ‘your\_other\_choice’, :action =\>‘else’) %\>  
\<% end %\>

hth,  
Bill
