A function that allows you to select a prefecture from the pull-down menu, which is often seen in questionnaires. , You can have the function to post using the form_with method. However, it is easier to use the prefecture if it is managed numerically on the table. In order to be able to manage with numerical values, it is necessary to prepare the prefecture name and the number associated with it in advance. ActiveHash is a convenient function in such cases.
Gem that allows you to handle data without saving it in the database by entering data that basically does not change in the model. Mainly used when you want to display as a pull-down menu in the view.
The official documentation is here
OK on the bottom line of the file
Gemfile
gem 'active_hash'
% bundle install
%rails g model model name--skip-migration
The model name should be such that the pull-down menus are grouped together.
ex) Baseball, soccer, tennis ...
When creating a pull-down like, the model name will be sport
.
By adding the --skip-migration option
, the migration file will not be generated. ActiveHash does not store in the database, so it does not require a migration file.
Hereinafter, the sports pull-down menu will be described as an example.
Change the model created in "3. Model generation" to the following description.
app/models/sport.rb
class Sport < ActiveHash::Base
self.data = []
end
ActiveRecord methods can be used by inheriting ActiveHash :: Base.
The contents of the pull-down are hashed into the array.
app/models/sport.rb
class Sport < ActiveHash::Base
self.data = [
{ id: 1, name: '---' },
{ id: 2, name: 'Baseball' },
{ id: 3, name: 'Soccer' },
{ id: 4, name: 'Tennis' }
]
end
Below is an explanation of the premise that there is already a table to store another post.
app/models/post.rb
class Post < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :sport
end
ʻExtend ActiveHash :: Associations :: ActiveRecordExtensions allows you to use the
belongs_to_active_hash method`.
There is no need to describe the association in the sport model.
* Note that the description is different from the normal association </ font>
app/models/post.rb
class Post < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :sport
validates :sport_id, presence: true #Unable to save empty post
validates :sport_id, numericality: { other_than: 1 }
#The pull-down selection is "--Cannot be saved when
end
collection_select(Column name to be saved,Array of objects,Items stored in columns,Column name displayed in the options,option,html option)
collection_select(:sport_id, Sport.all, :id, :name, {}, {class:"sport-select"})
Column name to be saved: In which column of the table to save.
Array of objects: If you want to display all the hashes of the array, you can use .all
.
Items saved in column: I want to save the part of ʻapp / models / sport.rb that was set to ʻid:
in the column.
Column name displayed in the options: Display the part of ʻapp / models / sport.rbthat was set as
name:` in the pull-down menu.
Optional: as needed
html options: class name, etc.
Anyway, the collection_select method has too many arguments, which can be confusing ...
Recommended Posts