How to match the same custom route for custom edit and update actions in Ruby on Rails 4?

时间:2016-04-25 08:55:46

标签: ruby-on-rails ruby-on-rails-4

I don't know how to set up my routes to have the following effect:

The image

What I have now are:

get 'change_password' => 'staffs#edit_password', as: :change_staff_password
patch 'change_password' => 'staffs#update_password', as: :change_staff_password

But it raises error: ArgumentError: Invalid route name, already in use: 'change_staff_password_staff'

The reason to do this is because the path is named "change_password_staff" without as:. However, I want it to be "change_staff_password" with staff in the middle for clarity.

I look up match and found match ":controller/:action/:id"

But how should I set up this to allow paths share same path name?

2 个答案:

答案 0 :(得分:1)

我找到了答案:

match 'change_password', to: 'staffs#edit_password', via: :get
match 'change_password', to: 'staffs#update_password', via: :patch

回答@hypern:我希望路线清晰简单。

答案 1 :(得分:0)

与建议的答案相比,这是另一种方法:

  resource :staffs, only: :index do
    get 'change_password' => "staffs#edit_password"
    patch 'change_password' => "staffs#update_password"
  end

only: :index仅作为示例,如果不需要,则应将其删除。