需要参与路由导航

时间:2017-03-24 10:10:48

标签: ruby-on-rails ruby

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

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

这是我的routes.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%>

有人可以告诉我这个

中的路由错误是什么

1 个答案:

答案 0 :(得分:1)

您需要在创建订单时将路线更改为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" %>