3

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 %>
Community
  • 1
  • 1
Brian M.
  • 170
  • 1
  • 1
  • 10
  • Can you show your `edit.html.erb` file? I suspect that's where the problem lies. – neuronaut Sep 18 '15 at 18:04
  • Edited the question to include both `edit.html.erb` and the `_form.html.erb` partial it uses. Thx! – Brian M. Sep 18 '15 at 18:26
  • It would seem that `form_for` is generating a "friendly" path, but you don't really want that, do you? Shouldn't it just go to `PATCH /blogs/:id` ? – zetetic Sep 18 '15 at 20:00
  • @zetetic - The idea was to leverage friendly_id to generate the slug in the desired URL structure. The only change I made to the _form partial was to remove the :slug field generated by the scaffold. – Brian M. Sep 18 '15 at 20:20
  • I don't mean to be rude but you should seriously learn how to distill your question down to the actual problem. This is a veeeery long read for such a simple issue. – Mike Szyndel Sep 19 '15 at 16:54
  • 1
    @MichalSzyndel - I appreciate your suggestion as I get the hang of SO best practices, so thanks! I thought I had succinctly presented the issue, thought it best that I share each file that had been altered (to prevent a someone from asking for add'l info), and outlined everything I tried to self-resolve. I'm sorry you found the result to be too long. I'll work on making future questions shorter. – Brian M. Sep 20 '15 at 22:52

1 Answers1

1

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
neuronaut
  • 2,689
  • 18
  • 24
  • THANKYOUTHANKYOUTHANKYOU @neuronaut! Everything works beautifully! And thanks for taking the time to explain it all too - I had no idea that leaving off the leading slash tells Rails that it's a relative path. – Brian M. Sep 18 '15 at 22:56