Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rails-engine/form-select
Rails form select improve
https://github.com/rails-engine/form-select
actionview form rails select
Last synced: 2 months ago
JSON representation
Rails form select improve
- Host: GitHub
- URL: https://github.com/rails-engine/form-select
- Owner: rails-engine
- License: mit
- Created: 2018-10-22T03:01:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-16T11:26:44.000Z (about 6 years ago)
- Last Synced: 2024-04-26T18:47:56.553Z (9 months ago)
- Topics: actionview, form, rails, select
- Language: Ruby
- Size: 46.9 KB
- Stars: 18
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# FormSelect
[![Build Status](https://travis-ci.org/rails-engine/form-select.svg?branch=master)](https://travis-ci.org/rails-engine/form-select)
[中文介绍与说明](https://ruby-china.org/topics/37668)
A simple improve for Rails form select helper.
We need use `form.select ::category_id, Category.collect { |record| [record.name, record.id] }` in so many case.
This gem give use a simple way.
## Usage
```rb
gem "form-select"
```app/model/category.rb
```rb
class Category
form_select :name, scope: -> { order("name asc") }
form_select :reverse_name, field: [:name, :id], scope: -> { order("name desc") }
endclass User
form_select :login, field: [:login, :to_param], scope: -> { order("id desc") }
form_select :email
form_select :email_value, field: :email
form_select :city, field: :city, scope: -> { where("city is not null").select(:city).distinct }def to_param
login.downcase
end
endclass Post
form_select :author, field: [:author], scope: -> { where("author is not null").select(:author).distinct }
end
```Now you got the helper methods:
```rb
irb> Category.name_options
=> [["Android", 3], ["iOS", 2], ["News", 1]]
irb> Category.reverse_name_options
=> [["News", 1], ["iOS", 2], ["Android", 3]]
irb> User.login_options
=> [["Foo", "foo"], ["Mike", "mike"], ["Jason", "jason"]]
irb> User.email_options
=> [["[email protected]", 10], ["[email protected]", 11], ["[email protected]", 12]]
irb> User.email_value_options
=> [["[email protected]", "[email protected]"], ["[email protected]", "[email protected]"], ["[email protected]", "[email protected]"]]
irb> User.city_options
=> [["NewYork", "NewYork"], ["San Francisco", "San Francisco"], ["Chicago", "Chicago"]]
irb> Post.author_options
=> [["Jason", "Jason"], ["Mike", "Mike"], ["Foor", "Foo"]]
```So you can easy use it in Rails form:
```erb
<%= form_with(model: post, local: true) do |form| %>
<%= form.label :title %>
<%= form.text_field :title %>
<%= form.label :user_id %>
<%= form.select :user_id, User.email_options %>
<%= form.label :city %>
<%= form.select :city, User.city_options %>
<%= form.label :category_id %>
<%= form.select :category_id, Category.name_options %>
<%= form.submit %>
<% end %>
```## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).