If / Else Form layout question

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

%>

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

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

Hi Jon,

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

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

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

Steve

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

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