7

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.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Simple, yet genius solution. Thank you very much. I should have guessed myself -.-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.