/YYYY/MM/Title-Slug URL 结构,在 #new 和 #edit 上带有 Friendly_Id 解决方案阻塞 [英] /YYYY/MM/Title-Slug URL structure with Friendly_Id Solution Chokes on #new and #edit
问题描述
我对我的上一问题有部分解决方案 正确显示了 posts#index 和 posts#show 路由,但在创建帖子后窒息:
I have a partial solution to my previous issue which is correctly displaying the posts#index and posts#show routes, but is choking after creating a post:
ActionController::UrlGenerationError in PostsController#create
没有路由匹配 {:action=>"show", :controller=>"posts"} 缺少必需的键:[:id, :month, :year]
ActionController::UrlGenerationError in PostsController#create
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id, :month, :year]
提取的源代码(围绕第 32 行):
30 respond_to do |format|
31 如果@post.save
32 format.html { redirect_to post_path, notice: '帖子已成功创建.'}
33 format.json { 渲染:显示,状态::创建,位置:@post }
34 其他
35 format.html { render :new }
Extracted source (around line #32):
30 respond_to do |format|
31 if @post.save
32 format.html { redirect_to post_path, notice: 'Post was successfully created.' }
33 format.json { render :show, status: :created, location: @post }
34 else
35 format.html { render :new }
...并编辑帖子:
没有路由匹配 [PATCH] "/blog/example-post/blog/2015/09/example-post"
No route matches [PATCH] "/blog/example-post/blog/2015/09/example-post"
以下是所有有问题的文件(在同一个非常简单的脚手架博客上工作):
Here are all the files in question (working off the same very simple scaffolded blog):
$ 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
routes.rb
Rails.application.routes.draw do
scope 'blog' do
get '', to: 'posts#index', as: 'posts'
post '', to: 'posts#create'
get '/new', to: 'posts#new', as: 'new_post'
get '/:id/edit', to: 'posts#edit', as: 'edit_post'
get '/:year/:month/:id', to: 'posts#show', as: 'post'
patch '/:id', to: 'posts#update'
put '/:id', to: 'posts#update'
delete '/:year/:month/:id', to: 'posts#destroy'
end
end
post.rb
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
def year
created_at.localtime.strftime("%Y")
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.all
end
def show
@post = Post.friendly.find(params[:id])
end
def new
@post = Post.new
end
def edit
@post = Post.friendly.find(params[:id])
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to post_path, 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_path, 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
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content, :slug)
end
end
end
posts_helper.rb
module PostsHelper
def post_path(post)
"blog/#{post.year}/#{post.month}/#{post.slug}"
end
end
app/views/posts/index.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_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 %>
总而言之,以下是有效的方法:
In summary, here's what works:
- /博客/索引
- /blog/2015/09/example-post
- 创建一个新帖子(直到当您收到上述 UrlGenerationError 时它应该重定向到帖子#show 的点)
- 也就是说,新帖子已添加到数据库中,因此如果您返回/index,新帖子将可见
...什么不起作用:
- 编辑帖子(带有表单的编辑页面将呈现,但在提交更改后,您将收到上述错误 - 并且更改永远不会进入数据库)
- 在创建新帖子后完成重定向(前面提到过).
- /blog/2015/index
- /blog/2015/09/index
我很高兴我已经做到了这一点 - 任何解决这些悬而未决问题的指导都将不胜感激!
I'm stoked that I've gotten this far - any guidance to resolving these outstanding issues would be really appreciated!
编辑
感谢@brad-werth,帖子创建已通过以下更改得到修复:
With thanks to @brad-werth, post creation has been fixed with the following change:
posts_controller.rb
def create @post = Post.new(post_params) respond_to do |format| if @post.save format.html { redirect_to post_path(@post.year, @post.month, @post), notice: 'Post was successfully created.' }
我还尝试通过以下方式解决帖子编辑问题:
I've also tried resolving the post edit problem in the following way:
将编辑路径更改为
get '/:year/:month/:id/edit', to: 'posts#edit', as: 'edit_post'
并将以下覆盖添加到 posts_helper.rb 以防止索引页面中断:Changed the edit route to
get '/:year/:month/:id/edit', to: 'posts#edit', as: 'edit_post'
and added the following override to posts_helper.rb to keep the index page from breaking:def edit_post_path(post) "#{post.year}/#{post.month}/#{post.slug}/edit" end
现在来自索引页面的编辑"链接将转到正确的 URL(
/blog/2015/09/example-post/edit
- 它曾经转到/blog/example-post/edit
) 并成功呈现编辑页面.但这会导致 PATCH 中断(实际上,更新不会进入数据库):And now the "edit" link from the index page is going to the correct URL (
/blog/2015/09/example-post/edit
- it used to go to/blog/example-post/edit
) and successfully renders the edit page. But this results in PATCH breaking (indeed, the updates don't make it to the DB):No route matches [PATCH] "/blog/2015/09/example-post/blog/2015/09/example-post"
我认识到这个重复问题可能与这个 edit_post_path 覆盖有关,但以下强制正确 PATCH 路由的尝试无效:
I recognize that this duplication problem likely lies with this edit_post_path override, but the following attempts to force the correct PATCH route have no effect:
- 更新 PATCH 路由到
patch '/:year/:month/:id', to: 'posts#update'
将更新后的 PATCH 路由命名为
为:'patch'
并将 PATCH 路径覆盖添加到 posts_helper:
- Update PATCH route to
patch '/:year/:month/:id', to: 'posts#update'
Name the updated PATCH route to
as: 'patch'
and add PATCH path override to posts_helper:
def patch_path(post) "#{post.year}/#{post.month}/#{post.slug}" end
将覆盖更改为:
Change the override to:
def patch_path(post) "" end
- 取消命名 &将 PATCH 路由更改为
patch '', 为:'posts#update'
查看posts_controller,看起来问题不存在,因为它不是重定向不是问题 - 我不明白为什么 @post.update(post_params)
会有问题:
Looking at the posts_controller, it doesn't look like the problem is there since it's not the redirect is not the problem - and I don't see why @post.update(post_params)
would be problematic:
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
因此,据我所知,URL 中的重复发生在 PATCH 操作之前,这使我们回到了 EDIT 流程 - 它必须将重复传递给 PATCH,在那里它结束了.想法?
So as far as I can tell the duplication in the URL is happening prior to the PATCH action, which brings us back to the EDIT flow - it must be passing the duplication to PATCH where it winds up choking. Ideas?
推荐答案
您的错误状态:
没有路由匹配 {:action=>"show", :controller=>"posts"} 缺少必需的键:[:id, :month, :year]
正如您从失败的行中看到的那样,format.html { redirect_to post_path, notice: '帖子已成功创建.'}
,您正在不带参数调用 post_path
.
As you can see from your failing line, format.html { redirect_to post_path, notice: 'Post was successfully created.' }
, you are calling post_path
with no arguments.
您的路线 get '/:year/:month/:id', to: 'posts#show', as: 'post'
需要年、月和 ID.这与您上面的错误消息一致.
Your route get '/:year/:month/:id', to: 'posts#show', as: 'post'
expects a year, month, and id. This jives with your error message above.
要修复,只需提供缺少的参数,如下所示:
To fix, simply supply the missing parameters, like so:
format.html { redirect_to post_path(@post.year, @post.month, @post),注意:'帖子创建成功.'}
这篇关于/YYYY/MM/Title-Slug URL 结构,在 #new 和 #edit 上带有 Friendly_Id 解决方案阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!