如何将自定义路由添加到资源路由

Sat*_*har 36 ruby-on-rails rails-routing ruby-on-rails-3

我有一个invoices_controller有资源路线.如下:

resources :invoices do
  resources :items, only: [:create, :destroy, :update]
end
Run Code Online (Sandbox Code Playgroud)

现在我想在发票中添加一个发送功能,如何添加一个自定义路由作为invoices/:id/send调度请求的说明invoices#send_invoice以及如何在视图中链接到它.

什么是传统的轨道方式来做到这一点.谢谢.

Dam*_*ien 41

在您的路线中添加:

resources :invoices do
  post :send, on: :member
end
Run Code Online (Sandbox Code Playgroud)

要么

resources :invoices do
  member do
    post :send
  end
end
Run Code Online (Sandbox Code Playgroud)

然后在你的意见:

<%= button_to "Send Invoice", send_invoice_path(@invoice) %>
Run Code Online (Sandbox Code Playgroud)

要么

<%= link_to "Send Invoice", send_invoice_path(@invoice), method: :post %>
Run Code Online (Sandbox Code Playgroud)

当然,您不依赖于POST方法