Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Translated options for rails form_for select

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 select_options.gemspec
README.rdoc

Select Options

This gem creates a select tag and a series of contained option tags for the provided object and method. Helper takes options from model's constant and translates it according to the translations file (`composite_attributes` section).

Installation

In Gemfile:

gem 'select_options'

Usage example

For example, you have a model User with the field role and a constant ROLES:

class User < ActiveRecord::Base
  ROLES = %w(admin user)
  ...
end

in the template you have just to write:

<%= f.select_with_options :role %>

In case if you have the constant, named different from the field name, like this:

class Shape < ActiveRecord::Base
  DIMENTION_VALUES = [2, 3]
  ...
end

in the template you should use option :source

<%= f.select_with_options :dimention, :source => Shape::DIMENTION_VALUES %>

Translations in en.yml for an ActiveRecord child look like this:

en:
  activerecord:
    composite_attributes:
      user:
        role:
          admin: Administrator
          user: User

or like this:

en:
  activerecord:
    attributes:
      shape:
        dimention_2: 2D
        dimention_3: 3D

This code will generate translated collection of options for select tag:

<select name="user[role]">
  <option value="admin" selected="selected">Administrator</option>
  <option value="user">User</option>
</select>

Copyright

Copyright © 2011 Dmitriy Vorotilin, Evrone.com. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.