Rails 4:简单自定义路由重定向以显示操作

时间:2014-02-05 10:24:29

标签: ruby-on-rails routes

我对铁路很新,我遇到路线问题。我正在尝试在两个不同的控制器之间链接视图。

在客户#index

<li><%= link_to "Manage Client Payments", payments_per_client_path %></li>

在付款控制器中:

  def payments_per_client
    @payment = Payment.all
  end

在路线档案中:

  resources :payments

  resources :direct_debits

  resources :clients do
    resources :payments
  end

   controller :payments do
    get 'payments/payments_per_client' => 'payments#payments_per_client', :as => :payments_per_client
   end

问题是“管理客户付款”的网址在我的浏览器中显示为:

/payments/payments_per_client

但链接重定向到付款#show。我不知道为什么会这样。

3 个答案:

答案 0 :(得分:0)

只需更改路径路径,然后删除控制器块

match 'payments/payments_per_client' => 'payments#payments_per_client', :as => :payments_per_client

答案 1 :(得分:0)

你需要重新排序路线

controller :payments do
    get 'payments/payments_per_client' => 'payments#payments_per_client', :as => :payments_per_client
end
resources :payments

resources :direct_debits

resources :clients do
   resources :payments
end

becoz您的resouces :payments已经有路由/付款/:ID,这是您的付款展示页面的路线,这就是为什么payments_per_clien被视为ID并将其重定向到显示页面的原因。< / p>

答案 2 :(得分:0)

您可以使用:

get 'payments/payments_per_client', to: 'payments#payments_per_client', as: :payments_per_client

match 'payments/payments_per_client' => 'payments#payments_per_client', as: :payments_per_client

match 'payments/payments_per_client' => 'payments#payments_per_client', as: :payments_per_client, via: :get