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

时间:2013-05-22 13:32:15

标签: ruby-on-rails ruby-on-rails-3 rails-routing

我有一个invoices_controller,它有资源路由。如下:

resources :invoices do
  resources :items, only: [:create, :destroy, :update]
end

现在我想在发票中添加一个发送功能,如何添加一个自定义路由invoices/:id/send,将请求发送到invoices#send_invoice,以及如何在视图中链接到该请求。

传统的导轨方式是什么?感谢。

5 个答案:

答案 0 :(得分:39)

在路线中添加:

resources :invoices do
  post :send, on: :member
end

或者

resources :invoices do
  member do
    post :send
  end
end

然后在你的观点中:

<%= button_to "Send Invoice", send_invoice_path(@invoice) %>

或者

<%= link_to "Send Invoice", send_invoice_path(@invoice), method: :post %>

当然,您不依赖于POST方法

答案 1 :(得分:2)

resources :invoices do
  resources :items, only: [:create, :destroy, :update]
  get 'send', on: :member
end

<%= link_to 'Send', send_invoice_path(@invoice) %>

它将转到send的{​​{1}}操作。

答案 2 :(得分:1)

match '/invoices/:id/send' => 'invoices#send_invoice', :as => :some_name

添加链接

<%= button_to "Send Invoice", some_name_path(@invoice) %>

答案 3 :(得分:1)

在Rails&gt; = 4中,您可以通过以下方式实现:

match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'

答案 4 :(得分:0)

资源:发票 在“:成员”上获取“发送” 结束