Rails button_to没有路线匹配{:action =>"complete",:controller =>"dashboard",:name =>"Order"} [英] Rails button_to No route matches {:action=>"complete", :controller=>"dashboard", :name=>"Order"}

查看:45
本文介绍了Rails button_to没有路线匹配{:action =>"complete",:controller =>"dashboard",:name =>"Order"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我尽力而为.我真的需要你的帮助.

Please help i have tried my best. I really need your help.

因此,我正在尝试将标记顺序设为完整.现在,所有操作都可以通过按钮完成,以将订单标记为完成.我进行了迁移以添加.

So im trying to make a mark order as complete. Now it all works up to the button to mark order as complete. I ran a migration to add.

class AddCompleteToOrder < ActiveRecord::Migration
  def change
    add_column :orders, :complete, :boolean, default: false
  end
end

然后我在order.rb中添加了一个方法

Then i added a method the order.rb of

def complete!
  update(complete: true)
end

然后路由.rb

resources :orders do
  post "complete", to: "orders#complete", on: :member
end

然后这是按钮

= button_to "Mark as complete", { action: "complete", id: @order.id }

但是我没有@ order.id 但是订单确实有一个@ order.name,所以我将其更改为

But i dont have a @order.id but a order does have a @order.name so i changed it to

= button_to "Mark as complete", { action: "complete", name: @order.name }

但是然后我得到了错误:

But then i get the error:

ActionController :: UrlGenerationError 显示/Users/jacksharville/Desktop/dancer/app/views/dashboard/dadmin.html.haml第87行出现的地方:

ActionController::UrlGenerationError in Dashboard#dadmin Showing /Users/jacksharville/Desktop/dancer/app/views/dashboard/dadmin.html.haml where line #87 raised:

没有路线匹配{:action =>"complete",:controller =>"dashboard",:name =>"Order"}

No route matches {:action=>"complete", :controller=>"dashboard", :name=>"Order"}

提取的来源(围绕第87行): 85 86 87

Extracted source (around line #87): 85 86 87

= link_to "Back to Dashboard", :back, :class => 'btn-danger btn'

= button_to "Mark as complete", { action: "complete", name: @order.name }

所以很明显我在把routes.rb做错了,但是我不能解决.请帮忙.任何帮助都将不胜感激.

So clearly im doing the routes.rb wrong but i cant fix it. Please help. Any help greatly appreciated.

routes.rb(完整文件)

routes.rb (full file)

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  get 'home/index'
  root 'home#index'
  #pages
  get '/why' => 'pages#why'
  get '/trak' => 'pages#trak'
  get '/contact' => 'pages#contact'
  get '/mydms' => 'pages#mydms'
  get '/air' => 'pages#air'
  get '/ocean' => 'pages#ocean'
  get '/road' => 'pages#road'
  get '/courier' => 'pages#courier'
  get 'fulfilment' => 'pages#fulfilment'
  get 'express' => 'pages#express'

  resources :dashboard
  get 'dadmin' => 'dashboard#dadmin'
  get 'myorders' => 'dashboard#myorders'
  get 'label' => 'dashboard#label'
  resources "contacts", only: [:new, :create]

  devise_for :users
  as :user do
    get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
    put 'users' => 'devise/registrations#update', :as => 'user_registration'
  end

  resources "orders"
  get "/confirm" => "confirmations#show"
  get 'dconfirmation' => 'orders#confirmation'

  resources :orders do
    post "complete", to: "orders#complete", on: :member
  end
end

orders_controller.rb

orders_controller.rb

class OrdersController < ApplicationController
  before_filter :authenticate_user!
  def new
    @order = Order.new
  end

  def create
    @order = current_user.orders.new(order_params)
    @order.email = current_user.email
    @order.name = current_user.name
    @order.address_line_1 = current_user.address_line_1
    @order.address_line_2 = current_user.address_line_2
    @order.postcode = current_user.postcode
    @order.city = current_user.city
    @order.country = current_user.country
    if @order.save
      redirect_to dconfirmation_path
    end
  end

  def order_params
    params.require(:order).
      permit(
        :email,
        :delivery_name,
        :company_name,
        :delivery_address1,
        :delivery_address2,
        :delivery_address3,
        :delivery_city,
        :delivery_postcode,
        :delivery_country,
        :phone,
        :package_contents,
        :description_content,
        :restricted_items,
        :terms_conditions,
        :insurance,
        :contents_value,
        :cf_reference,
        :reference_number
        )
  end
  def show
    @user = User.find(params[:id])
  end

  def confirmation
  end

  def complete!
    order = Order.find(params[:id])
    order.complete!
    # handle response
  end

end

dashboard_controller.rb

dashboard_controller.rb

class DashboardController < ApplicationController

  before_filter :authenticate_user!

  def index
  end

  def admindashboard
    (current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
  end

  def adminuser
    (current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
  end

  def dadmin
    (current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
    # @order = Order.all
    @order = Order.order("name").page(params[:page]).per(1)
  end

  def myorders
    @order = current_user.orders.order("name").page(params[:page]).per(1)
  end

  def show
    @user = User.find(params[:id])
  end

  def label
    @order = current_user.orders.order("name").page(params[:page]).per(1)
  end

  def complete!
    order = Order.find(params[:id])
    order.complete!
    # handle response
  end
end

推荐答案

您需要将complete操作从DashboardsController移到OrdersController,这是在路由文件中定义的位置.

You need to move the complete action from DashboardsController to OrdersController, which is where it is defined in the routes file.

此外,您可能可以使用:

Also, you can probably use:

= button_to "Mark as complete", complete_order_path(@order)

控制器动作正在寻找params[:order_id],因此它可以找到正确的顺序.我不确定在您的视图中如何定义订单,但是您需要将订单对象传递到路径.

The controller action is looking for params[:order_id] so it can find the right order. I am not sure how orders are defined in your view, but you will need to pass an order object to the path.

如果您有机会在Order类中定义了一个to_param方法,该方法将订单的名称而不是id定义为参数,则您将需要更新complete操作以按名称查找订单.

If by any chance you defined a to_param method in Order class, which defines order's name rather than id as params, you will need to update the complete action to look for an order by name.

但是我猜这是默认的id.因此,将订单对象传递到路径应该可以.

But my guess is that it is the default id. So passing the order object to the path should work.

这篇关于Rails button_to没有路线匹配{:action =&gt;"complete",:controller =&gt;"dashboard",:name =&gt;"Order"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆