如何在路由帮助程序中发送 HTTP 方法?



如何在路由帮助程序中发送HTTP方法?即我想从控制器操作调用以下链接:

<%= link_to 'Submit to Scheduling', orders_path(cart_id: @cart), method: :post, data: { confirm: "Are you sure?" }, class: "primary button btn" %> 

我试过orders_path(cart_id: @cart)但没有用。

orders_path(cart_id: @cart, method: :post)也没有

我该怎么做?

编辑 1

orders_path GET /orders(.:format)     orders#index
POST    /orders(.:format) orders#create
new_order_path  GET /orders/new(.:format) orders#new
edit_order_path GET /orders/:id/edit(.:format) orders#edit
order_path  GET /orders/:id(.:format) orders#show
PATCH   /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE  /orders/:id(.:format) orders#destroy

很遗憾,您无法通过redirect_to方法发送帖子方法。您必须使用 get 方法考虑它。简单的方法是制作另一个页面,您可以在其中获取 Get 中的所有参数,并且该页面可以使用 form_url 发送 post 方法。但我仍然不会考虑它,因为在 get 中发送参数然后将它们带到发布方法似乎是安全问题。 但这仍然是您的电话,因此以下是使用另一个页面的方法。Anotehr 方法是添加一个 gem 重新发布您的Gemfile

gem repost

现在使用以下方法发送重定向

redirect_post(orders_path(order_id: @order))

它将自动发送 post 请求,但请记住您必须修复令牌验证问题,并在控制器中使用以下行禁用它

skip_before_action :verify_authenticity_token

至于纯粹是视图作业的警报框,所以如果你也想要它,那么你必须创建实际的视图和控制器来重定向并显示这个弹出警报消息。

最新更新