ruby-on-rails Rails,如何将订单标记为已完成

lxkprmvk  于 2023-02-26  发布在  Ruby
关注(0)|答案(2)|浏览(129)

我需要一种方法来将我制定的方法应用于我选择的订单。或者找到另一种方法来将某些内容标记为完成。
这是我目前为止尝试过的。但我不介意尝试一些全新的东西。所以我有这些订单,我在视图中显示如下:

.col-md-12
  .admindash
    %h1 Admin Dashboard
    .text-norm
      Welcome to the admin dashboard here you can see incoming and outgoing orders.

.row
  .col-md-8
    - for order in @order
      %h2
        = order.user.name
      .orderpanel
        .clientarea
          Client name:
          = order.user.name
          %br
          Client email:
          = order.user.email
          %br
          Client address ... -->:
          %br
          = order.user.address_line_1
          %br
          = order.user.address_line_2
          %br
          = order.user.postcode
          %br
          = order.user.city
          %br
          = order.user.country
        .delivarea
          %br
          delivery_name:
          = order.delivery_name
          %br
          company_name:
          = order.company_name
          %br
          delivery_address1:
          = order.delivery_address1
          %br
          delivery_address2:
          = order.delivery_address2
          %br
          delivery_address3:
          = order.delivery_address3
          %br
          delivery_city:
          = order.delivery_city
          %br
          delivery_postcode:
          = order.delivery_postcode
          %br
          phone:
          = order.phone
          %br
          package_contents:
          = order.package_contents
          %br
          description_content:
          = order.description_content
          %br
          contents_value:
          = order.contents_value
          %br
          cf_reference:
          = order.id
          CR
          %br
          reference_number:
          = order.reference_number
        .tac
          restricted_items:
          = order.restricted_items
          %br
          terms_conditions:
          = order.terms_conditions
          %br
          insurance:
          = order.insurance
          = order.id
  .col-md-4.sidebar
    = paginate @order
    = link_to "Data Control", rails_admin_path, :class => 'btn-danger btn'
    %p
    = link_to "Back to Dashboard", :back, :class => 'btn-danger btn'

我想在页面上添加一个按钮,这样我就可以将订单标记为完成,也许可以制作一个包含所有订单的表,并添加一个按钮,显示“标记为完成”。
所以我运行了一个迁移来添加布尔类complete

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

然后在命令控制器我:

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

order.rb

class Order < ActiveRecord::Base
  belongs_to :user

  def complete!
    update(complete: true)
  end

end

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'
end

所以我现在需要一些方法来更新一个特定的订单。比如list allorder.id,然后选择那个订单来使用那个方法。我不知道我在做什么,因为我是Rails的新手。请帮助我。我只需要指示一个订单现在已经完成。
我不介意我如何标记订单为完成,它只需要记住这一点,如果页面被重新加载,我的网站确实使用jQuery.有什么方法可以通过我做?

unhi4e5o

unhi4e5o1#

有几个选项,但这里有一个让您的控制器休息。
First -我使用日期时间而不是布尔值。

class AddCompleteToOrder < ActiveRecord::Migration
  def change
    add_column :orders, :completed_at, :datetime
  end
end

在控制器中,只使用update方法,但让它response_to js

respond_to do |format|
  format.html { redirect_to [:admin, @order] }
  format.js { render :update }
end

创建一个update.js.erb(或haml)视图,看起来你正在使用haml。

- order_list_item = render :partial => 'admin/orders/order', :locals => { :order => @order }
$("##{dom_id(order)}").replaceWith('#{j(order_list_item)}')

您可以部分重复使用订单。
在order partial中,添加一个按钮或链接。我想你也可以使用button_to来代替。

= link_to "Completed", admin_order_path(order, 'order[completed]' => true), :remote => true, :method => :put

记住要把dom_id添加到your _order.html.haml partial中。Haml提供了一个方便的方法来完成这个任务。
把它移到它自己的分部中,所以

- for order in @order

你会的

# /admin/orders/_order.html.haml

%div[order]
  ... All the other order code from the earlier for loop

然后这样称呼它

= render @orders  # Change your instance variable name to "@orders" to match rails conventions

在模型中,处理完成和完成=

class Order < ActiveRecord::Base

  def completed
    !completed_at.nil?
  end

  def completed=(state=false)
    completed_at = Time.now if state
  end

end

如果你一开始没有看到页面更新,请检查开发者工具(chrome或safari)的响应输出,这样你就可以在响应正文中看到错误。

41ik7eoe

41ik7eoe2#

在订单_控制器.rb中

def completed_orders
    # Get order from DB
    @order = Order.find(params[:id])

    if !@order.completed?
      if @order.completed!
        flash[:notice] = "Your order has been marked complete. Email notification will be sent!" 
        # Can also do more stuff here.
      else
        flash[:alert] = "Something is hmmm not right..."
        # Can also do more stuff here.
      end
    end
    redirect_to request.referrer
  end

在订单模型order.rb中,您可以枚举,将status列作为整数添加到orders表中。下面您从0到1进行枚举。这样,您就可以简单地知道status列中的0是“处理中”,1将是“完成”。您还可以在orders.rb中添加更多的枚举。您还可以使用布尔值,如0为false,1为true。但这有限制,只有两个.所以你最好有状态列作为一个整数和枚举,你可以添加更多像“处理,正在调查,其他问题,已完成."在这里下面它只是两个状态.

belongs_to :user, required: false
enum status: [:processing, :completed]

确保你的路线在你完成订单操作后已经更新。也许你需要重新迁移,或者更新你的数据库等等。

相关问题