ekampp / rails-for_select

A simple way to use activerecord in rails select_tags

Home Page:https://rubygems.org/gems/rails-for_select

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rails for_select

Build Status

Simplifies delivering content to a rails form select_tag by exposing the for_select class-method on any class inheriting from ActiveRecord::Base.

User.for_select 
# => [["name", 1], ["other name", 2], ...]

For those models that doesn't defaultly have a name, or that should use another id field, you can customize it like this.

User.for_select(id: :uuid) 
# => [["name", 'uuid1'], ["other name", 'uuid2'], ...]

User.for_select(name: :full_name) 
# => [["full name", 1], ["other full name", 2], ...]

If you're records doesn't have timestamps you can use another cache key.

User.for_select(cache_key: :created_on) 
# => [["name", 1], ["other name", 2], ...]

Putting it all together with a rails form

= form_for Post do |f|
  = f.select :author, User.for_select

About

A simple way to use activerecord in rails select_tags

https://rubygems.org/gems/rails-for_select


Languages

Language:Ruby 100.0%