/YYYY/MM/Title-Slug URL 结构,带有 Friendly_Id 解决方案阻塞 #edit [英] /YYYY/MM/Title-Slug URL structure with Friendly_Id Solution Chokes on #edit

查看:76
本文介绍了/YYYY/MM/Title-Slug URL 结构,带有 Friendly_Id 解决方案阻塞 #edit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我在 之前的问题 解决我的实现/YYYY/MM/Slug URL 结构的原始问题,我希望得到一些帮助来解决我在尝试编辑帖子时收到的错误:

<块引用>

没有路由匹配 [PATCH] "/blog/2015/09/example-post/blog/2015/09/example-post"

以下是所有有问题的文件(在同一个非常简单的脚手架博客上工作):

$ rails 新博客[...]$ cd 博客# (添加friendly_id到Gemfile & install)$ rails 生成friendly_id$ rails 生成脚手架帖子标题内容 slug:string:uniq[...]$耙数据库:迁移

routes.rb

Rails.application.routes.draw 做范围博客"做get '', to: 'posts#index', as: 'posts'post '', to: 'posts#create'get '/new', to: 'posts#new', as: 'new_post'get '/:year/:month/:id/edit', to: 'posts#edit', as: 'edit_post'get '/:year/:month/:id', to: 'posts#show', as: 'post'补丁 '/:id', to: 'posts#update'把'/:id',到:'posts#update'删除 '/:year/:month/:id', to: 'posts#destroy'结尾结尾

post.rb

class Post 

posts_controller.rb

class PostsController <应用控制器before_action :set_post, only: [:show, :edit, :update, :destroy]定义索引@posts = Post.all结尾高清秀@post = Post.friendly.find(params[:id])结尾定义新@post = Post.new结尾定义编辑@post = Post.friendly.find(params[:id])结尾定义创建@post = Post.new(post_params)response_to do |格式|如果@post.saveformat.html { redirect_to post_path(@post.year, @post.month, @post),注意:'帖子创建成功.'}format.json { 渲染:显示,状态::创建,位置:@post }别的format.html { 渲染:新}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾定义更新response_to do |格式|如果@post.update(post_params)format.html { redirect_to post_path,注意:'帖子已成功更新.'}format.json { 渲染:显示,状态::ok,位置:@post }别的format.html { 渲染:编辑}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾销毁@post.destroyresponse_to do |格式|format.html { redirect_to posts_url,注意:'帖子已成功销毁.'}format.json { 头:no_content }结尾结尾私人的# 使用回调在动作之间共享公共设置或约束.def set_post@post = Post.friendly.find(params[:id])结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.def post_paramsparams.require(:post).permit(:title, :content, :slug)结尾结尾结尾

posts_helper.rb

模块 PostsHelperdef post_path(post)博客/#{post.year}/#{post.month}/#{post.slug}"结尾def edit_post_path(post)#{post.year}/#{post.month}/#{post.slug}/edit"结尾结尾

app/views/posts/index.html.erb

<%= notice %>

<h1>发布帖子</h1><表格><头><tr><th>Title</th><th>内容</th><th>Slug</th><th colspan="3"></th></tr></thead><% @posts.each 做 |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><%结束%></tbody><br><%= link_to '新帖子', new_post_path %>

总而言之,以下是有效的方法:

  • /博客/索引
  • /blog/2015/09/example-post
  • 创建新帖子
  • 销毁帖子

...什么不起作用:

  • 编辑帖子(带有表单的编辑页面将呈现,但在提交更改后,您将收到上述错误 - 并且更改永远不会进入数据库)

我认识到这个重复问题可能与这个 edit_post_path 覆盖有关,但以下强制正确 PATCH 路由的尝试无效:

  1. 更新 PATCH 路由到 patch '/:year/:month/:id', to: 'posts#update'
  2. 将更新后的 PATCH 路由命名为 为:'patch' 并将 PATCH 路径覆盖添加到 posts_helper:

    def patch_path(post)#{post.year}/#{post.month}/#{post.slug}"结尾

  3. 将覆盖更改为:

    def patch_path(post)"结尾

  4. 取消命名 &将 PATCH 路由更改为 patch '', 为:'posts#update'

查看posts_controller,看起来问题不存在,因为它不是重定向不是问题 - 我不明白为什么 @post.update(post_params) 会有问题:

 def 更新response_to do |格式|如果@post.update(post_params)format.html { redirect_to @post,注意:'帖子已成功更新.'}format.json { 渲染:显示,状态::ok,位置:@post }别的format.html { 渲染:编辑}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾

因此,据我所知,URL 中的重复发生在 PATCH 操作之前,这使我们回到了 EDIT 流程 - 它必须将重复传递给 PATCH,在那里它结束了.想法?提前致谢!

编辑

edit.html.erb

编辑帖子

<%= 渲染表单"%><%= link_to 'Show', @post %>|<%= link_to '返回',posts_path %>

_form.html.erb

<%= form_for(@post) do |f|%><% 如果@post.errors.any?%><div id="error_explanation"><h2><%=pluralize(@post.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @post.errors.full_messages.each 做 |message|%><li><%=消息%></li><%结束%>

<%结束%><div class="field"><%= f.label :title %><br><%= f.text_field :title %>

<div class="field"><%= f.label :content%><br><%= f.text_field :content %>

<div class="actions"><%= f.submit %>

<%结束%>

解决方案

看起来主要问题出在 helper 上.返回的路径需要以斜线开头:

模块 PostsHelperdef post_path(post)"/blog/#{post.year}/#{post.month}/#{post.slug}"结尾def edit_post_path(post)"/blog/#{post.year}/#{post.month}/#{post.slug}/edit"结尾结尾

如果没有开头的斜杠,浏览器会将它们视为相对路径,将它们附加到当前路径(这就是为什么你最终得到 /blog/2015/09/example-post/blog/2015/09/example-post 提交编辑表单时).

您还需要确保您的 patchput 路由一致:

get '/:year/:month/:id', to: 'posts#show', as: 'post'补丁 '/:year/:month/:id', to: 'posts#update'把 '/:year/:month/:id', to: 'posts#update'删除 '/:year/:month/:id', to: 'posts#destroy'

最后,控制器需要包含 PostsHelper 并在 createupdate 中使用其重定向 URL 的方法:

class PostsController <应用控制器包括 PostsHelper...定义创建@post = Post.new(post_params)response_to do |格式|如果@post.saveformat.html { redirect_to post_path(@post),注意:'帖子已成功创建.'}format.json { 渲染:显示,状态::创建,位置:@post }别的format.html { 渲染:新}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾定义更新response_to do |格式|如果@post.update(post_params)format.html { redirect_to post_path(@post),注意:'帖子已成功更新.'}format.json { 渲染:显示,状态::ok,位置:@post }别的format.html { 渲染:编辑}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾...结尾

Based on guidance I got in my earlier question in resolving my original issue to implement /YYYY/MM/Slug URL structure, I'm hoping to get some help in resolving an error I'm receiving when attempting to edit a post:

No route matches [PATCH] "/blog/2015/09/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     '/:year/:month/: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(@post.year, @post.month, @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_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

  def edit_post_path(post)
    "#{post.year}/#{post.month}/#{post.slug}/edit"
  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/index
  • /blog/2015/09/example-post
  • Creating a new post
  • Destroying a post

…What doesn't work:

  • Editing a post (the edit page with the form will render, but after submitting your changes you'll get the aforementioned error - and the changes never make it to the DB)

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:

  1. Update PATCH route to patch '/:year/:month/:id', to: 'posts#update'
  2. 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
    

  3. Change the override to:

    def patch_path(post)
      ""
    end
    

  4. Un-name & change PATCH route to patch '', to: 'posts#update'

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

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? Thanks in advance!

EDIT

edit.html.erb

<h1>Editing Post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

_form.html.erb

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
        <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_field :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

解决方案

It looks like the primary problem is in the helper. The paths returned need to begin with a slash:

module PostsHelper
  def post_path(post)
    "/blog/#{post.year}/#{post.month}/#{post.slug}"
  end

  def edit_post_path(post)
    "/blog/#{post.year}/#{post.month}/#{post.slug}/edit"
  end
end

Without the beginning slash the browser treats them as relative paths, tacking them on to the current path (which is why you end up with /blog/2015/09/example-post/blog/2015/09/example-post when submitting the edit form).

You'll also need to make sure that your patch and put routes are consistent:

get     '/:year/:month/:id',      to: 'posts#show',   as: 'post'
patch   '/:year/:month/:id',      to: 'posts#update'
put     '/:year/:month/:id',      to: 'posts#update'
delete  '/:year/:month/:id',      to: 'posts#destroy'

And lastly the controller needs to include PostsHelper and use its methods for the redirect URLs in create and update:

class PostsController < ApplicationController
  include PostsHelper
...
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to post_path(@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_path(@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
...
end

这篇关于/YYYY/MM/Title-Slug URL 结构,带有 Friendly_Id 解决方案阻塞 #edit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆