In my application_controller I’ve added:
def instantiate_controller_and_action_names
@current_action = action_name
@current_controller = controller_name
end
so that I show/hide certain fields in my views depending on the current action. For example, when adding a new user, I want the ability to show a password field to be filled out, but when editing a user, I don’t want the password field to display.
The problem I’m having is that @current_action only returns a value of ‘new’ when creating a new record, but isn’t returning a value when I’m editing a record (e.x.- http://localhost:3000/users/6/edit), or if I use the inverse and check if the @current_action != ‘new’…in both cases my field is still being displayed.
<%if @current_action != 'new'%>
<tr><td>AAAAAAA</td></tr>
<%else%>
<tr>
<th class="">Password</th>
<th class="">Password Confirmation</th>
</tr>
<tr>
<td><%= f.password_field :password %></td>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<%end%>
What is happening that ‘edit’ isn’t being returned and/or is there a better/best practice way to accomplish this? Thanks!
Rather than use the controller action, why do you not test your object? You can see if your object is a new record or not with this method