设置POST,GET PUT,PATCH,Rails 5的路由名称

时间:2017-10-15 20:56:36

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

我正在使用Devise并更改其默认路由:

devise_for :users, :path => '',skip: [:sessions]

as :user do
    get '/forgotten' => 'users/passwords#new', as: :password
    post '/forgotten' => 'users/passwords#create'
    put '/forgotten' => 'users/passwords#update'
    get '/password/edit' => 'users/passwords#edit'
end

new.html.erb生成的网址(表单操作属性)(使用新密码说明发送电子邮件的视图)为/forgotten.user,这是不正确的。正确的URL是/forgotten,它与不同的HTTP动词GET / POST / PUT一起使用。  请求新密码表单(new.html.erb):

<%= form_for(resource, as: resource_name, 
             url: password_path(resource_name), html: { method: :post}) 

如果没有rake routes的自定义路线,我会看到以下路线:

  

user_password |补丁| /password(.:format)|用户/密码#更新

     

PUT | /password(.:format)|用户/密码#更新

     

POST | /password(.:format)|用户/密码#创建

如何使此路由自定义并用于不同的HTTP谓词?

1 个答案:

答案 0 :(得分:0)

尝试删除as :user do

get '/forgotten' => 'users/passwords#new', as: :password
post '/forgotten' => 'users/passwords#create'
put '/forgotten' => 'users/passwords#update'
get '/password/edit' => 'users/passwords#edit'

如果您需要像/user/forgotten那样使用scope 'user' do