Reputation: 431
I'm making a form that will have several fields and one of them should have default value wich is not visible if the resource is being created. But if it is being edited the field should be shown. So I try something like this:
<%= form_for(@task) do |f| %>
<div class="field" id="v_field">
<%= f.label :v_field, "Always visible field." %>
<%= f.text_field :status %>
</div>
<% if params[:action] != "new" %>
<div class="field" id="default_field">
<%= f.label :default_field, "Default field (should be invisible for new resources only)." %>
<%= f.text_field :status %>
</div>
<% end
<% end %>
But it doesn't work. Also I've tried to change controller settings like this:
format.html { :except => [:default_field] }
But it doesn't work.
Please tell me, what comparison should I use in the condition? Thanks.
Upvotes: 7
Views: 2110
Reputation: 14018
If you're following normal Rails conventions, you'll display this form either via the new
or edit
methods in the task controller.
For new, a new (empty) task is created, for edit an existing one is fetched from the database.
A simple test then would be to see if the task has an id
yet.
<% if @task.id %>
...
<% end %>
You're better off looking at the state of the object your manipulating rather than at the actions the user took to get there.
Upvotes: 8