A type of gem that allows hash data to be used in the same way as ActiveRecord. Data that is basically unchanged, such as prefectures, does not need to be saved in the database, and if the data is written directly in the view file, it lacks readability. Use active_hash in such situations.
Creating a sports article posting app using a simple Active Hash (CSS is omitted this time)
gem 'active_hash'
Next, create a model. This time, we will create two models, a sports model and a genre model. First, create a sports model with rails g model.
rails g model sports
Next, create a genre model.
rails g model genre --skip-migration
This time --skip-migration is an option to not generate the migration file when creating the model file.
Next, edit genre.rb. At this time, inherit the ActiveHash :: Base class. By inheriting ActiveHash :: Base, you can use methods similar to ActiveRecord. Result Suppose you write the following object.
class Genre < ActiveHash::Base
self.data = [
{ id: 1, name: '--' },
{ id: 2, name: 'baseball' },
{ id: 3, name: 'Football' },
{ id: 4, name: 'tennis' },
{ id: 5, name: 'Table tennis' },
{ id: 6, name: 'basketball' },
{ id: 7, name: 'land' },
{ id: 8, name: 'Sports' }
]
end
Genre data is stored in an array in hash format.
Also, by adding a column called genre_id to the sports model migration file and managing the genre model (ActiveHash) id as an external key in the sports table, you will be able to acquire the genre associated with that article. ..
class CreateSports < ActiveRecord::Migration[6.0]
def change
create_table :sports do |t|
t.string :title , null: false
t.text :text , null: false
t.integer :genre_id , null: false t.timestamps
end
end
end
$ rails db:create
$ rails db:migrate
Since active_hash has a belongs_to_active_hash method, Describe this method in sports.rb and define the association.
class Sports < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :genre
end
Add validation to sports.rb earlier.
class Sports < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :genre
#Prevent empty posts from being saved
validates :title, :text, presence: true
#The genre selection is "--When "", it cannot be saved
validates :genre_id, numericality: { other_than: 1 }
end
Rails.application.routes.draw do
root to: 'sportses#index'
resources :sportses
end
rails g controller sportses index new
articles_controller.rb
class ArticlesController < ApplicationController
def index
@sportses = Sports
end
def new
end
def create
@sports = Sports.new(sports_params)
if @sports.save
redirect_to root_path
else
render :new
end
end
private
def sports_params
params.require(:sports).permit(:title,:text,:genre_id)
end
end
app/views/articles/index.html.erb
<h1>List of articles</h1>
<%= link_to "Post", new_sports_path, class:"post" %>
<% @sportses.each do |sports| %>
<div class="sports">
<div class="sports-genre">
<%= sports.genre.name %>
</div>
<div class="sports-title">
<%= sports.title %>
</div>
<div class="sports-text">
<%= sports.text %>
</div>
</div>
<% end %>
It is an implementation of the article posting screen.
app/views/articles/new.html.erb
<%= form_with model: @sports, url:sportses_path, local: true do |f| %>
<div class="sports-box">
Post an article
<%= f.text_field :title, class:"title", placeholder:"title" %>
<%= f.text_area :text, class:"text", placeholder:"text" %>
<%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {class:"genre-select"}) %>
<%= f.submit "Post" ,class:"btn" %>
</div>
<% end %>
By using collection_select, you can use the active hash in pull-down format. collection_select exists from the first argument to the fifth argument, and the arguments are input in the following order. The data (array) of genre.rb is specified by Genre.all. The sport name actually selected by the user in the view is saved in the sports model as genre_id.
<%= form.collection_select(Column name to be saved,Array of objects,Items stored in columns,Column name displayed in the options,option,html option) %>
<%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {class:"genre-select"}) %>
For the first time, I made a full-scale output so far, so I think there were many parts that could not be reached. I will edit it to make it easier to understand, but by writing this article, I was able to sort out my understanding of active hashes.
Recommended Posts