Dog
Dog

Reputation: 2906

Hardcode partial input value in a Rails form_for field

I have a Rails form where the user is able to make a Top5 list. While they are able to label the list, all lists start with the phrase "Top 5 Greatest." Of course, it's easy to use Regex and append this phrase to the front of a list in the controller, but then I'm stuck doing it for both the create and update actions, so it's not very dry. Is there a way to hardcode a partial value in a rails form_for that will then be combined with the rest of the form input and sent to the controller?

This is currently what the form looks like (minus the form_for partial):

  <br><%= f.label "Top Five Greatest:" %>
  <%= f.text_field :title %><br>
  <%= f.hidden_field :user_id, value: user.id %>
  <br><%= f.label "#1" %>
  <%= f.text_field :number1 %>
  <br><%= f.label "#2" %>
  <%= f.text_field :number2 %>
  <br><%= f.label "#3" %>
  <%= f.text_field :number3 %> 

I'd really appreciate any insight.

Upvotes: 1

Views: 214

Answers (1)

eugene
eugene

Reputation: 550

Override setter method: # put this in your model:

def title=(value)
    super("Top 5 Greatest #{value}")
end

Upvotes: 2

Related Questions