Search code examples
ruby-on-railsruby-on-rails-4custom-routes

Rails 4 form_for giving incorrect action for patch route


I'm building a simple CMS as a first exercise to learn Rails. I've created a Pages model and adjusted the routes so the show action is domain.com/:id and all the other actions are domain.com/admin/pages/:id. My routes look as I expect when I run rake routes and everything is working except when I edit a page the action in the form is incorrect and I get a No route matches [PATCH] error. If I hardcode the the form action to what I expect it to be it all works correctly. However I don't want to do that as I have the same form for New & Update. Here's the details:

This is the form tag in my view

<%= form_for @page  do |f| %>¬

Which for New/Post produces

# <form action="/admin/pages" all good
<form class="new_page" id="new_page" action="/admin/pages" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="authenticity_token" value="[..token..]">
  # <!-- regular form fields -->
  <input type="submit" name="commit" value="Save" class="btn btn-primary">
</form>

But for Update/Patch produces

# <form action="/1" should be /admin/pages/1
<form class="edit_page" id="edit_page_6" action="/6" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="_method" value="patch">
  <input type="hidden" name="authenticity_token" value="[..token...]">
  # <!-- Regular Form Fields --> 
  <input type="submit" name="commit" value="Save" class="btn btn-primary">  
</form>

My Routes File

root 'pages#home'
resources :pages, :only => [:show,:home], :path => ''
resources :pages, :except => [:show,:home], :path => 'admin/pages'

Which produces the following with rake routes:

   Prefix Verb   URI Pattern                     Controller#Action
     root GET    /                               pages#home
     page GET    /:id(.:format)                  pages#show
    pages GET    /admin/pages(.:format)          pages#index
          POST   /admin/pages(.:format)          pages#create
 new_page GET    /admin/pages/new(.:format)      pages#new
edit_page GET    /admin/pages/:id/edit(.:format) pages#edit
          PATCH  /admin/pages/:id(.:format)      pages#update
          PUT    /admin/pages/:id(.:format)      pages#update
          DELETE /admin/pages/:id(.:format)      pages#destroy

Update - pages_controller.rb

class PagesController < ApplicationController
  before_action :set_page, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_admin!, except:[:show,:home]
  layout "admin"

  def index
    @pages = Page.all
  end

  def home
    @page = Page.find(1) #todo: make dynamic
    render :show
  end

  def show
    redirect_to_good_slug(@page) and return if bad_slug?(@page)
    render layout: "layouts/public"
  end

  def new
    @page = Page.new
  end

  def edit
    # @page defined in set_page method below
  end

  def create
    @page = Page.new(page_params)

      if @page.save
        redirect_to edit_page_path(@page), notice: '...success'
      else
        render :new
      end
  end

  def update
      if @page.update(page_params)
        redirect_to @page, notice: 'Page was successfully updated.'
      else
        render :edit
      end
  end

  def destroy
    @page.destroy
      redirect_to pages_url, notice: 'Page was successfully destroyed.'
  end

  private
    def set_page
      @page = Page.find(params[:id])
    end

    def page_params
      params.require(:page).permit(:title, :slug, :content, :published)
    end
end

Solution

  • This is occurring because you altered the show action path.

    rails form_for will try to use create action path if the resource is not persisted. otherwise it will use show action path.

    This means When editing a resource, rails form_for uses the path of show action.

    Solutions

    1. Alter the route for show action also.

      resources :pages, :except => [:home], :path => 'admin/pages'

    2. Try this in edit form. but little hackish way of doing.

      <%= form_for @page, :url => "#{pages_path}/#{@page.id}" do |f| %>