Rails - default value in text_field but only for new_record?

Posted by jyoseph on Stack Overflow See other posts from Stack Overflow or by jyoseph
Published on 2011-01-09T04:27:00Z Indexed on 2011/01/09 4:53 UTC
Read the original article Hit count: 260

Filed under:

On a Content model have an attribute named slug. When creating a new record, I want to use a helper to populate this field, but on an existing record I want to use the value from the database.

Currently I have:

<%- if @content.new_record? -%>
  <%= f.text_field :slug, :value => "#{generate_slug(6)}" %>
<%- else %>
  <%= f.text_field :slug %>
<%- end %>

But that seems a bit verbose. Is this the best way, or is there no other way? (Rails newb just trying to find the "Rails way" on issues I'm unsure of)


Edit

I should note that the helper is currently in /app/helpers/application_helper.rb

© Stack Overflow or respective owner

Related posts about ruby-on-rails