Search code examples
ruby-on-railsrubyformtastic

How to make the label title and input value the same, but id is different


I have question, how i can make the label title and input value the same, but id is different. Code below show what i want.

<%= f.input :building_type, :label_html => {:class => "ui-priority n1"}, 
    label: "Building type", collection: Post::Flat::BUILDING_TYPES, as: :radio %>


Post::Flat::BUILDING_TYPES = [["Flat-super", 1], ["Flat-middle", 2]]

Output code:

<div class="radio">
  <label class="choice" for="post_flat_building_type-1">
    <input id="post_flat_building_type-1" name="post_flat[building_type]" type="radio" value="1">
    Flat-super
  </label>
</div>
<div class="radio">
  <label class="choice" for="post_flat_building_type-2">
    <input id="post_flat_building_type-2" name="post_flat[building_type]" type="radio" value="2">
    Flat-middle
  </label>
</div>

But i want to make value equals to Flat-super, Flat-middle, how i can achieve this ?

Thank you for any help!

Formtastic version - 2.3 Formtastic-bootstrap version - 3.0

----- Some addition----

Someone tells that i can solve this problem by change initial array to

Post::Flat::BUILDING_TYPES = [["Flat-super", "Flat-super"], ["Flat-middle", "Flat-middle"]]

But it not work if i have cyrillic

Post::Flat::BUILDING_TYPES = [["Кирпичный", "Кирпичный"]

<div class="radio">
  <label class="choice" for="post_flat_building_type">
   <input id="post_flat_building_type" name="post_flat[building_type]" type="radio" value="кирпичный">
    кирпичный
  </label>
</div>

You can observe that output contain wrong id, it affects if i have more than 1 object in my array. All inputs will have the same id.


Solution

  • If problem occurs only when creating checkboxes and radiobuttons, try to hack formtastic, transliting characters from cyrrilic to latin when creating attribute id of input and attribute for of label.

    module Formtastic
      module Inputs
        module Base
          module Choices
    
            def choice_html_safe_value(choice)
              name = Translit.convert(choice_value(choice).to_s, :english)
              name.gsub(/\s/, '_').gsub(/[^\w-]/, '').downcase
            end
    
          end # Choices
        end
      end
    end
    

    You can use gem translit or make translit by yourselves. After hacking, formtastic will generate something like this:

    <div class="radio">
      <label class="choice" for="post_flat_building_type_kirpichnyj">
        <input id="post_flat_building_type_kirpichnyj" name="post_flat[building_type]" type="radio" value="кирпичный">
        кирпичный
      </label>
    </div>