需要帮助在轨道布线



我正在尝试在名为 orders_controller.rb 的控制器中调用某个方法"custom"但是我的路由中有一个错误,那就是这个

No route matches {:action=>"custom", :controller=>"orders"} missing required keys: [:id, :id]

这是我的路线.rb 文件

Rails.application.routes.draw do
  resources :categories
  devise_for :users,:controllers => { :registrations => "registrations" }
  resources :products
    resource :cart, only: [:show] do
        post "add", path: "add/:id",on: :member
        get :checkout
    end
     resource :results, only: [:index] do
        get :index
    end
    resources :comments
    resource :home, only: [:index] do
        get :index
    end
    resources :orders, only:[:show,:index,:create] do
        post "custom", path: "custom/:id",on: :member
    end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    get 'results/index'
    get 'comments/index'
    root 'home#index'
end

这是我的orders_controller.rb

class OrdersController < ApplicationController
    before_filter :initialize_cart
    def create
        @order_form=OrderForm.new(user: User.new(order_params[:user]),cart: @cart)
        if @order_form.save
            redirect_to products_path,notice:"Thank You for placing the order sir."
        else
        render "carts/checkout"
        end
    end
    def custom
       @order_form=OrderForm.new(user: current_user,cart: @cart) 
          if @order_form.save
            redirect_to products_path,notice:"Thank You for placing the order sir."
        else
        render "carts/checkout"
        end
    end
   private
    def notify_user
      OrderMailer.order_confirmation(@order_form.order).deliver  
    end
    def order_params
        params.require(:order_form).permit(
            user:[:name,:phone,:address,:city,:country,:postal_code,:email])
    end
end
在这里,我想在单击">

下订单"按钮时调用自定义方法,但由于页面未打开而无法单击它

这是我在views/cart/checkout.html.erb中页面的主要代码

 <%if current_user %>
    <%= button_to 'Place order',custom_order_path,:class => "btn btn-success btn-lg" %> 
    <%else%>
<div style="width:70%;margin:20px auto;">
    <%=render "errors"%>
    <%=render "form"%>
    <%end%>

有人可以告诉我这是什么路由错误吗

您需要

在创建订单时将路由更改为collection_route

resources :orders, only: [:show, :index, :create] do
  post :custom, on: :collection
end

现在您可以使用

<%= button_to 'Place order',custom_order_path, :class => "btn btn-success btn-lg" %> 

最新更新