Problems with "devise_for", "devise_scope"!

I am trying to set up my routes in development. I tried using devise_scope: user, but it does not work. So I switched to devise_for and skipped the user routes (registrations, confirmations, passwords, session), and it worked. But then an error appears in my views when I called, for example, "session_path". He made the form redirect to "session.user", which makes no sense.

Here is the code:

  # routes.rb

   devise_for: users,: path => '',: skip => [: passwords,: registrations,: confirmations] do
     post "account / password",: to => "devise / passwords # create"
     get "account / password / new",: to => "devise / passwords # new",: as => "new_password"
     get "account / password / edit",: to => "devise / passwords # edit",: as => "edit_password"
     put "account / password",: to => "devise / passwords # update"
     post "account",: to => "users / registrations # create"
     get "sign_up",: to => "users / registrations # new"
     delete "account",: to => "users / registrations # destroy"
     post "confirmation",: to => "devise / confirmations # create"
     get "confirmation / new",: to => "devise / confirmations # new",: as => "new_confirmation"
     get "confirmation",: to => "devise / confirmations # show"
   end

New session view:

  # users / sessions / new.html.erb
   = form_for (resource,: as => resource_name,: url => session_path (resource_name)) do | f |  %>

Error:

  No route matches {: action => "new",: format =>: user,: controller => "devise / passwords"}

What should I do? What happened to "devise_scope" does not work correctly (the error it was showing was "Could not find graphic display for ...")?

thanks

+4
source share
3 answers

What routes are being created? Take rake routes to find out.

If you do not see the routes that you expect, try defining your own routes in the block after the devise_for statement. For instance,

devise_for :users, skip => [ :passwords, :registrations, :confirmations] as :user do post "account/password" => "devise/passwords#create" get "account/password/new" => "devise/passwords#new" get "account/password/edit" => "devise/passwords#edit" put "account/password" => "devise/passwords#update" ... end 

You may also need to customize the views to point to the new paths that you have announced. Take a look at the wiki Wiki to learn how to customize your views.

+5
source

try adding :controllers => { :passwords => "devise/passwords"} option

+1
source

I checked your routes and I got this on rake routes :

 account_password POST /account/password(.:format) {:controller=>"devise/passwords", :action=>"create"} new_password GET /account/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"} edit_password GET /account/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"} PUT /account/password(.:format) {:controller=>"devise/passwords", :action=>"update"} account POST /account(.:format) {:action=>"create", :controller=>"users/registrations"} sign_up GET /sign_up(.:format) {:action=>"new", :controller=>"users/registrations"} DELETE /account(.:format) {:action=>"destroy", :controller=>"users/registrations"} confirmation POST /confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"} new_confirmation GET /confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"} GET /confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"} new_user_session GET /sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"} user_session POST /sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"} 

destroy_user_session DELETE / sign_out (.format) {: action => "destroy" ,: controller => "devise / sessions"}

So, I think you should use user_session_path (without any arguments) on form_for . If you put the user as an argument, Rails will think it is a format and it will not work.

+1
source

Source: https://habr.com/ru/post/1346093/


All Articles