Rahul
Rahul

Reputation: 1876

Assign custom labels to checkboxes using Formtastic

I have a few checkboxes coming from nested model (:admin accepts_nested_attributes_for :account_setting). By default, the label for the checkbox is generated from the model's attributes. But I want to use custom labels which I store in the locales folder as key value pairs.

This is the code,


= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
  = f.input :email
  = f.inputs :receive_email_digest, :as =>:check_boxes, :for => :account_setting, :label => 'My custom label'

And it does not work. I tried :input_html, :member_label.

Does formtastic suppor this? Or do we have to hack it?

Upvotes: 2

Views: 1966

Answers (1)

sj26
sj26

Reputation: 6833

inputs is a fieldset. You're telling formtastic to make a fieldset and ordered list with one list item and input, :receive_email_digest, which I presume is a boolean. The :as here is not really having any effect, and :check_boxes is for has_many associations.

You want something like:

= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
  = f.inputs do
    = f.input :email
  = f.inputs :for => :account_setting do
    = f.input :receive_email_digest, :label => 'My custom label'

which will render two fieldsets each with a single input. Or perhaps even:

= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
  = f.inputs "Email Settings" do
    = f.input :email
    = f.semantic_fields_for :account_setting do |ff|
      = ff.input :receive_email_digest, :label => 'My custom label'

The semantic fields for here is merely a scope and shouldn't output any markup.

I believe both of these will use en.formtastic.labels.<resource_name>.account_setting.receive_email_digest for the check box's label, where resource name is whatever your resource's underscored name is. Check out the formtastic source for how these keys are generated.

Upvotes: 1

Related Questions