Rails 4 form_for为补丁路由提供不正确的操作

时间:2015-05-28 03:26:14

标签: ruby-on-rails ruby-on-rails-4 custom-routes

我正在构建一个简单的CMS作为学习Rails的第一个练习。我创建了一个Pages模型并调整了路由,因此show action是domain.com/:id,所有其他操作都是domain.com/admin/pages/:id。当我运行rake routes并且一切正常时,我的路线看起来像我预期的那样,除非我编辑页面时表单中的操作不正确并且我收到No route matches [PATCH]错误。如果我将表单操作硬编码到我期望的那样,那么一切正常。但是我不想这样做,因为我对New&更新。这是详细信息:

这是我视图中的表单标记

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

New/Post生成

# <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>

Update/Patch生成

# <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>

我的路线档案

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

使用rake路线生成以下内容:

   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

更新 - 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

1 个答案:

答案 0 :(得分:4)

这是因为您更改了show操作路径。

如果资源未保留,

rails form_for将尝试使用create操作路径。否则它将使用show行动路径。

这意味着在编辑资源时,导轨form_for会使用show操作的路径。

<强>解决方案

  1. 也改变show行动的路线。

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

  2. 在编辑表单中尝试此操作。但是很少有做事的方式。

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