1

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?

Community
  • 1
  • 1
Brian M.
  • 170
  • 1
  • 1
  • 10

2 Answers2

0

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.' }

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • I just noticed you have a helper named post_path. This is overriding your route, maybe on purpose. You need to pull it, or call it like `post_path(@post)`. Defining methods that overlap route helpers might not be a great plan, for maintainability. – Brad Werth Sep 18 '15 at 05:56
  • Your patch error is the same basic thing, note that your route is only expecting the id, not everything else. – Brad Werth Sep 18 '15 at 05:57
  • Thanks for the suggestions @brad-werth. Passing in the missing params into the 'create' redirect worked. Unfortunately, the 'update' is still failing with the same change. – Brian M. Sep 18 '15 at 15:21
  • As for pulling the routing override, as far as I can tell this is the only way to get the /YYYY/MM/slug URL structure to work. If you know of an alternative way that is better for maintainability, I'd be happy to go that way. – Brian M. Sep 18 '15 at 15:23
  • @BrianM. I address the update bit in the comments. It would really be better as a separate question, but I think you have enough information to solve this one. I'd rather help you learn than spoon feed you a solution. Look at the route you are targeting, it only has the id parameter. You either need to build it out, or adjust the named route parameters... – Brad Werth Sep 18 '15 at 15:31
  • I appreciate you wanting to help me help myself (that's what I want too, but sometimes you get a little anxious when you've been hitting your head against the wall on the same problem for hours ;-) ). I've updated the original question to outline my attempts to resolve the edit problem. – Brian M. Sep 18 '15 at 17:00
  • @BrianM. I'm not sure updating the original question is the best way to get a solution. The original question was about a specific error. It also mentioned another error, but without detail. You have replaced the original detail with the heretofore absent details regarding the second issue. I feel like these would have been better as very specific separate questions. I am not inclined to answer questions that evolve over the course of several days. These questions are usually not useful to others, and are frustrating to answerers. I'm afraid I do not have any more time to invest into this one. – Brad Werth Sep 18 '15 at 17:24
  • http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions – Brad Werth Sep 18 '15 at 17:26
  • With all due respect @brad-werth, the original question had two errors; the first of which you've helped me resolve (thanks!). Consequently, I don't believe this to be a case of a "chameleon question" if I'm updating what I'm trying to resolve the second error based on your guidance. That said, I can appreciate your position about having the question addressing one error and not two. I had assumed (yes, I acknowledge the "assume" saying) that since it applied to the same source desire that it made sense to include them both. I appreciate the time and help you've shared - thanks! – Brian M. Sep 18 '15 at 17:39
0

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 ...' }
Florin Ionita
  • 271
  • 1
  • 6