views:

93

answers:

4

I have a Rails form for editing objects of a particular class. The labels on the form need to vary slightly depending on which particular object is being edited.

The obvious way to do this would be to add an instance method to the model objects to return the appropriate label for that object. However, this would appear to be putting part of the view's implementation in the models folder of my Rails project, and that doesn't seem right.

Is there a better way to do this?

+1  A: 

Not sure if I totally understand, but it seems like you could a helper method for this.

rpflo
Helpers would work until I decided to subclass the object, and then it gets very messy.
grahamparks
+1  A: 

My general guideline is that text is fine in the model unless it contains markup. If the text contains markup it belongs in a helper.

Andy Gaskell
A: 

You put all your labels in your localized files (even if you have only English), and then query from there see i18n guide, section 5.1

If you use form generation plugin like formtastic, it let you define labels and form hints right there on the file (from the readme):

2. Add some cool label-translations/variants (config/locale/en.yml):

  en:
    formtastic:
      labels:
        post:
          title: "Choose a title..."
          body: "Write something..."
      hints:
        post:
          title: "Choose a good title for you post."
          body: "Write something inspiring here."
amikazmi
A: 

Wouldn't the best way be to use a view helper?

e.g:

def modified_label_tag(obj, field); .... code to generate label_tag; end

Keeps presentational logic out of the model and business logic out of the view.

Whatever you do, do not override label_tag!

Even if you have a subclass, does it matter?

e.g:

label_class = case obj.class
when SubClassOfModel
'foo'
...
end
Omar Qureshi