Friendly_ID
, , /:year/:month/:tags - :
scope "/blog" do
resources :year, controller :blog_posts, only: :show, path: "" do
resources :month, controller : blog_posts, only: :show, path: "" do
resources :title, controller: blog_posts, only: :show, path: ""
end
end
end
resources :blog_posts, path: :blog -> domain.com/blog/new
, , Rails (domain.com/blog/...) blog_posts#show
:
Class BlogPostsController < ApplicationController
def show
case true
when params[:year].present?
@posts = Post.where "created_at >= ? and created_at < ?", params[:year]
when params[:month].present?
@posts = Post.where "created_at >= ? and created_at < ?", params[:month]
when params[:id].present?
@posts = Post.find params[:id]
end
end
end
<% if @posts %>
<% @posts.each do |post| %>
<%= link_to post.title, blog_post_path(post) %>
<% end %>
<% end %>
<% if @post %>
<%= link_to post.title, blog_post_path(post) %>
<% end %>
-
, friendly_id:
Class BlogPost < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
end
( , , ), , , , , / year/month/title