-->

/YYYY/MM/Title-Slug URL structure with Friendly_Id

2020-04-21 06:56发布

问题:

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
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id, :month, :year]

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 }

…and editing a 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/index
  • /blog/2015/09/example-post
  • Creating a new post (up until the point where it's supposed to redirect to posts#show when you get the UrlGenerationError mentioned above)
    • That said, the new post is added to the DB so if you go back to /index the new post will be visible
  • 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)
  • Completing the redirect after creating a new post (mentioned earlier).
  • /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!

EDIT

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:

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

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"

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?

回答1:

Your error states:

No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id, :month, :year]

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.

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), notice: 'Post was successfully created.' }



回答2:

in update and create actions change:

format.html { redirect_to post_path, notice: 'Post was successfully ...' }

with

format.html { redirect_to @post, notice: 'Post was successfully ...' }