请帮助我尽我所能。我真的需要你的帮助。
所以我试图让标记顺序完整。现在一切都可以通过按钮将订单标记为完成。我运行了迁移添加。
class AddCompleteToOrder < ActiveRecord::Migration
def change
add_column :orders, :complete, :boolean, default: false
end
end
然后我添加了一个方法order.rb
def complete!
update(complete: true)
end
然后routes.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所以我把它改成了
= button_to "Mark as complete", { action: "complete", name: @order.name }
但后来我得到了错误:
Dashboard #dadmin中的ActionController :: UrlGenerationError 显示/Users/jacksharville/Desktop/dancer/app/views/dashboard/dadmin.html.haml第87行:
没有路线匹配{:action =&gt;“complete”,:controller =&gt;“dashboard”,:name =&gt;“Order”}
提取的来源(第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错误但我无法解决它。请帮忙。非常感谢任何帮助。
routes.rb(完整档案)
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
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
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
答案 0 :(得分:0)
您可以在button_to
代码:controller => "order"
中指定此附加选项,但使用button_to
代码的正确方法是不为选项创建单独的哈希。而是将其用作button_to("Mark as complete", controller: "order", action: "complete", name: @order.name)
答案 1 :(得分:0)
试试这个
button_to "Mark as complete", { controller: "orders", action: "complete", name: @order.name }, {method: :post}
答案 2 :(得分:0)
此链接可以提供帮助:Routing Error - No route matches when using button_to with custom action
rake路线给我:
complete_order POST /order/:id/complete(.:format) orders#complete
答案 3 :(得分:0)
您需要将complete
操作从DashboardsController
移至OrdersController
,这是路径文件中定义的位置。
此外,您可以使用:
= button_to "Mark as complete", complete_order_path(@order)
控制器操作正在寻找params[:order_id]
,以便它可以找到正确的顺序。我不确定您的视图中是如何定义订单的,但您需要将订单对象传递给路径。
如果您在to_param
类中定义了一个Order
方法,它定义了订单的名称而不是id作为参数,则需要更新complete
操作以查找按名称排序。
但我的猜测是它是默认的id
。因此,将订单对象传递给路径应该有效。