October 19

0 comments

Ruby on Rails – Show a field only if the controller is ‘new’

By Christopher Mendla

October 19, 2016


Last Updated on November 30, 2019 by Christopher G Mendla

There can be times when you want to show a field, or do some other action, only for a  certain controller action such as ‘new’.

In this example, we have a form and we want to the currently logged in user (@current_user_name in this case) if this is a new complaint. However if we are editing we want to show the name of the user in the user table (@user.name)


<% if action_name ‘new’ then %>      <%= @current_user.name %><% else %>      <%= @user.name %><% end %>

Christopher Mendla

About the author

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}