Last active
March 28, 2022 14:39
-
-
Save secretpray/6a7281b922862fda6c861dceef43265b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------- model Recipe ------------- | |
1) Controllers: Recipes | |
======================= | |
class RecipesController < ApplicationController | |
after_action :register_visit, only: :show | |
attr_accessor :recipe | |
helper_method :recently_recipes | |
def recently_recipes | |
return [] if recently.blank? # [] if recently.none? | |
Recipe.where(id: recently) | |
end | |
def recently | |
session[:viewed_recipes] ||= [] | |
end | |
private | |
def register_visit | |
session[:viewed_recipes] ||= [] | |
session[:viewed_recipes] = ([@recipe.id] + session[:viewed_recipes]).uniq.take(3) | |
end | |
2) Views: recipes#show | |
====================== | |
<!-- Recently views--> | |
<% unless recently_recipes.none? %> | |
<h3 class='mt-5'><%= t '.recently' %></h3> | |
<div class='col-md-9 jumbotron my-3'> | |
<div class='row mt-3' id='dashboard-container'> | |
<%= render partial: 'recipes/recipe_dashboard', locals: { recipes: recently_recipes } %> | |
</div> | |
</div> | |
<% end %> | |
3) Localization: | |
================ | |
en.yml | |
show: | |
recently: 'Recently viewed' | |
ru.yml | |
show: | |
recently: 'Недавно проcмотренные' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment