・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction -Login function implementation
** * Be careful to specify the version! (An error occurred if not specified) **
Gemfile
gem 'impressionist', '~>1.6.1'
Terminal
$ bundle
Terminal
$ rails g impressionist
Terminal
$ rails db:migrate
schema.rb
create_table "impressions", force: :cascade do |t|
t.string "impressionable_type"
t.integer "impressionable_id"
t.integer "user_id"
t.string "controller_name"
t.string "action_name"
t.string "view_name"
t.string "request_hash"
t.string "ip_address"
t.string "session_hash"
t.text "message"
t.text "referrer"
t.text "params"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index"
t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index"
t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index"
t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index"
t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index"
t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index"
t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index"
t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index"
t.index ["user_id"], name: "index_impressions_on_user_id"
end
Terminal
$ rails g migration AddImpressionsCountToUsers impressions_count:integer
~_add_impressions_count_to_users.rb
class AddImpressionsCountToUsers < ActiveRecord::Migration[5.2]
def change
# 「default:Added "0"
add_column :users, :impressions_count, :integer, default: 0
end
end
Terminal
$ rails db:migrate
user.rb
#Postscript
is_impressionable counter_cache: true
is_impressionable
➡︎ Allow ʻimpressionist` to be used in the User model.
** * Please be sure to add counter_cache: true
. ** **
users_controller.rb
def index
@users = User.order(impressions_count: 'DESC') #Added sorting function
end
def show
@user = User.find(params[:id])
impressionist(@user, nil, unique: [:session_hash]) #Postscript
end
User.order(impressions_count: 'DESC')
➡︎ Sort the user list in descending order of the number of PVs.
impressionist(@user, nil, unique: [:session_hash])
➡︎ When you access the user details page, the number of PVs increases by one.
[When measuring by IP address]
impressionist(@tourist_spot, nil, unique: [:impressionable_id, :ip_address])
** * The number of PVs can be counted only once per user so that the number of PVs cannot be increased voluntarily. ** **
slim:users/index.html.slim
table.table
thead
tr
th
|Ranking
th
|Full name
th
|Number of PV
tbody
- @users.each.with_index(1) do |user, index|
tr
td
= index
|Rank
td
= link_to user do
= user.name
td
/Display the number of PV
= user.impressions_count
Recommended Posts