simple_form_for与自定义控制器操作

时间:2015-12-14 21:43:57

标签: ruby-on-rails routes simple-form-for

我的用户索引视图中的这一行:

<%= simple_form_for current_user, :url => user_change_client_path,
                                  :method => :patch,
                                  :id => current_user.id do |f| %>

生成以下错误:

No route matches {:action=>"change_client", :controller=>"users"} missing required keys: [:user_id]

我在控制器中定义了change_client操作。在我的routes.rb中,我有:

resources :users, only: [:index] do
  patch 'change_client'
  patch 'approve'
end

Rake路线显示:

user_change_client PATCH  /users/:user_id/change_client(.:format)   users#change_client
user_approve PATCH  /users/:user_id/approve(.:format)               users#approve

导致错误的错误是什么?

1 个答案:

答案 0 :(得分:1)

您的路线如下:

user_change_client PATCH  /users/:user_id/change_client(.:format)

需要user_id参数。当您调用此帮助程序时,将当前用户传递给它,如下所示:

user_change_client_path(current_user)