我真的希望有人可以帮助这个 Rails n00b 解决这个问题。在过去的几天里,我一直在研究、尝试、崩溃(和燃烧)如何为我正在整理的博客实现标准 /YYYY/MM/Title-Slug URL 结构。我发现并成功实现了Friendly_Id来处理sluggification(以及历史跟踪),但我一生都无法解决路由问题的年/月部分。
在我忘记之前:我正在使用 Rails 4.2.3 和 Ruby 2.2.1p85 (因为,是的,我利用了 RailsTutorial.org 中的一堆东西):-)
为了最大程度地减少混乱(或附带损害),我搭建了一个超级简单的博客应用程序来尝试使其全部正常工作:
$ rails new blog
[...]
$ cd blog
# (Add friendly_id to Gemfile & install)
$ rails generate friendly_id
$ rails generate scaffold post title content slug:string:uniq
[...]
$ rake db:migrate
进行了以下更改post.rb:
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
def year
created_at.localtime.year
end
def month
created_at.localtime.strftime("%m")
end
end
posts_controller.rb:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.order('created_at DESC').all
end
def show
end
def new
@post = Post.new
end
def edit
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_post
@post = Post.friendly.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, :published_at, :slug)
end
end
索引.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Slug</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= post.slug %></td>
<td><%= link_to 'Show', post_date_path(post) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Post', new_post_path %>
路线.rb:
Rails.application.routes.draw do
get '/posts', to: 'posts#index', as: :posts_path
get '/posts/:year', to: 'posts#index', as: :posts_year,
constraints: { year: /\d{4}/ }
get '/posts/:year/:month', to: 'posts#index', as: :posts_month,
constraints: { year: /\d{4}/, month: /\d{1,2}/ }
get '/posts/:year/:month/:slug', to: 'posts#show', as: :post_date,
constraints: { year: /\d{4}/, month: /\d{1,2}/, slug: /[a-z0-9\-]+/ }
resources :posts
end
这些更改主要来自更新 Rails3 代码Stackoverflow 问答 https://stackoverflow.com/questions/11569876/rails-3-2-friendly-url-routing-by-date因为这让我在我发现的其他选择中走得最远。我目前遇到以下控制器异常:
Showing […]/app/views/posts/index.html.erb where line #24 raised:
No route matches {:action=>"show", :controller=>"posts", :month=>nil, :slug=>nil, :year=>#<Post id: 23, title: "test", content: "", slug: "test-4", created_at: "2015-09-01 21:05:48", updated_at: "2015-09-01 21:05:48">} missing required keys: [:month, :slug, :year]
其他以其他稍微令人心碎的方式失败的解决方案:
- “Rails 4 Blog /:year/:month/:title with clean Routing”(请参阅链接的评论)- 这似乎不起作用,因为4.1.2 似乎从未修复过的错误 https://github.com/rails/rails/issues/16058)
- “Rails 4.1.2 - to_param 转义斜杠(并破坏应用程序)”(请参阅链接的评论) - 这可能有效,但我无法根据我的目的翻译答案
- “Friendly_Id slugs,其 id 或日期用斜杠分隔”(请参阅链接的评论)
需要明确的是:我并不执着于这种方法——我非常乐意走一条完全不同的路。我希望我的最终博客的功能如下:
-
http://www.example.com/blog/
(对于索引)
-
http://www.example.com/2015/
(针对 2015 年帖子索引)
-
http://www.example.com/2015/09/
(查看 2015 年 9 月的帖子索引)
-
http://www.example.com/2015/09/pleeze-help-me
(对于单个帖子)
提前谢谢了!
EDIT
在深入一些额外的兔子洞来解决这个问题时,我想知道使用 URL 重写是否是唯一的方法?针对这个问题的方法。我的直觉告诉我,它正在将一个圆钉敲入一个方孔(特别是考虑到该博客尚未上线,因此不可能有指向当前 URL 结构的链接),但我失败了寻找更好的替代方案。
我发现了两个可能有助于重写方法的选项:折射(请参阅链接的注释)和机架重写(请参阅链接的注释)
有人对这种替代方法和/或这些插件有任何意见吗?
Thanks!
PS - SO 权限似乎有更新,现在需要至少 10 声誉才能发布 2 个以上链接,因此我必须删除所有链接才能发布此编辑。我已将它们移至评论中,以便保存编辑内容。