Today, I implemented an implementation that adds a pull-down to the new registration screen for creating my own portfolio! It's a feature I really wanted to do, so if you're happy when you can do it, then. The feelings when this happens are addictive lol Aside from this. I would like to roughly output the implementation procedure.
Suppose you have data that is basically unchanged. There is no need to store it in the database as it is basically unchanged data. On the other hand, if you write those data directly in a view file etc., it will not be readable. Active Hash is useful in such cases.
gem 'active_hash'
First, install this gem. (At the bottom of the Gemfile) Then do bundle install.
This time we will implement a pull-down on the new registration screen, so create a user model as usual. From here, make another model for pull-down. In my case this time, I will make a fan_history model for pulling down about the fan history of sports.
% rails g model fan_history --skip-migration
What is different from usual is "--skip-migration". This is an option to prevent the migration file from being generated when creating the model file. This time, the fan history information of the article is not saved in the database, so there is no need to create a migration file.
3, ActiveHash::Base ActiveHash :: Base will be able to use methods similar to ActiveRecord. By inheriting ActiveHash :: Base, you can use ActiveRecord methods for objects defined in the Genre model.
fun_history.rb
class FanHistory < ActiveHash::Base
self.data = [
{ id: 1, name: '--' },
{ id: 2, name: '0~1 year' },
{ id: 3, name: '1~5 years' },
{ id: 4, name: '5~Decade' },
{ id: 5, name: '10 years or more' }
]
end
I will write it like this. There are two points you should be aware of, so please check them. The first is that the first line is not ActiveHash :: Base by default! The second point is that the last comma is not written in the id: 5 line! This is a place I want you to be careful about lol It's a small place, but I didn't notice it and it stagnated a little. Lol By the way, if you want to pull down the prefectures, it is very troublesome to describe, so I think it is better to get it from the net.
20201029085540_devise_create_users.rb
t.string :nickname, null: false
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.integer :fan_history_id, null: false
There are two points to be aware of here as well. The first is that _id is added to save the id in the users table! The second is that it was originally a string, but it has been changed to an integer! It's a pattern, so if you understand it, it's easy to remember. Don't forget db: migrate
user.rb
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :fan_history
validates :fan_history_id, numericality: { other_than: 1 }
Just remember to write the first line in must. I will omit the explanation of the meaning. The second line describes the normal association with active_hash. The third line prevents you from saving if the pull-down is "-" selected. If you are writing from id1 of fan_history.rb, normal validation is fine.
new.html.erb
<%= f.collection_select(:fan_history_id, FanHistory.all, :id, :name, {}, {class:"fan_history-select"}) %>
Connect with routing and controller, and finally write this description in the newly registered view. This is also a pattern, so it was easy to remember. The shape is like this.
new.html.erb
<%= form.collection_select(Column name to be saved,Array of objects,Items stored in columns,Column name displayed in the options,option,html option) %>
The amount of work is relatively large, but I felt that it was very easy to remember in terms of flow. If it is the first implementation, it will take time and I think it is natural and an error may occur, but it is relatively difficult to understand and I think that there will be no error that will be stuck for hours, so when an error occurs, typo or flow You might think it's a mistake inside. In order not to increase the number of tables wastefully, if the data to be handled is basically unchanged, let's use ActiveHash.
Recommended Posts