gpt4 book ai didi

ruby-on-rails - Ruby on Rails AASM 更改按钮单击时的转换

转载 作者:行者123 更新时间:2023-12-03 01:25:53 25 4
gpt4 key购买 nike

我的问题非常简单:如何更改按钮单击时的 aasm 转换?我应该在我的观点中加入什么?

我有两个按钮:批准拒绝。我的状态如下所示:

  aasm :column => 'state' do
state :pending, :initial => true
state :approved
state :rejected

event :approve do
transitions :to => :approved, :from => [:pending]
end

event :reject do
transitions :to => :rejected, :from => [:pending]
end
end

更新:我的参数是这些:

{"utf8"=>"✓",
“_method”=>“放置”,
"authenticity_token"...",
“提交”=>“应用程序”,
“id”=>“65”}

这就是我从 View 访问操作的方式:

= form_for([:admin, shop], method: :put, data: { confirm: "You sure?" }) do |f|
= f.submit "Approve", :controller => :shops, :action => :approve

我的 Controller 代码:

def approve
@shop = Shop.find(params[:id])
@shop.approve!
end

路线:

namespace :admin do
get "shops/:id/approve" => "shops#approve"

最佳答案

正如 Srikanth 所提到的,您需要将请求发送到 Controller ,但是,我建议使用 button_to,而不是直接使用 form如下:

<%= button_to "Approve", admin_shops_approve_path(shop.id), method: :put %>
<%= button_to "Disapprove", admin_shops_disapprove_path(shop.id), method: :put %>

这将向您的 Controller 发送请求。您似乎已经在这样做,但为了澄清起见,您需要使用以下内容:

#config/routes.rb
namespace :admin do
resources :shops do
put :approve #-> domain.com/admin/shops/:id/approve
put :disapprove #-> domain.com/admin/shops/:id/disapprove
end
end

#app/controllers/admin/shops_controller.rb
class Admin::ShopsController < ApplicationController
before_action, :set_shop

def approve
@shop.approve!
end

def disapprove
@shop.disapprove!
end

private

def set_shop
@shop = Shop.find params[:id]
end
end

这将为您提供一种将所需数据发送到表单的有效方法,从而触发 AASM 流程。

关于ruby-on-rails - Ruby on Rails AASM 更改按钮单击时的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25892656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com