[ruby-on-rails] Devise 로그인을 루트 페이지로 설정

내 경로에 다음 코드를 사용하고 있습니다.

devise_for :user,
  :as => '',
  :path_names => {
    :sign_in => "",
    :sign_out => "logout",
    :sign_up => "register"
  }

그러나 로그 아웃하고 /logout이동하면 다음 오류가 발생합니다.

{: action => “new”, : controller => “devise / sessions”}와 일치하는 경로가 없습니다.

작업 할 루트 경로를 어떻게 설정 :sign_in합니까?



답변

오류에 대해 묻는 사람들의 후속 Could not find devise mapping for path "/"조치에는 해결 방법이 있습니다.

로그에 다음과 같은 단서가 있음을 알 수 있습니다.

[Devise] Could not find devise mapping for path "/".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    match "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]

그래서 접근 방식을 다시 시도했지만 대신 범위 블록 내부에 (@miccet suggets로) 래핑했습니다.

devise_scope :user do
  root to: "devise/sessions#new"
end

이것은 나를 위해 잘 작동했습니다.


답변

devise_for :users

devise_scope :user do
  authenticated :user do
    root 'home#index', as: :authenticated_root
  end

  unauthenticated do
    root 'devise/sessions#new', as: :unauthenticated_root
  end
end

이와 마찬가지로 Rails Rails 4.1.0.rc1에서 테스트되었습니다.


답변

root :to => "devise/sessions#new"

기본 홈 루트를 설정해야했습니다. 어제 밤새도록 (질문을 게시하기 전에) 이것을 시도한 것처럼 느꼈지만 지금은 작동하고 있습니다. 로그 아웃 한 경우 Devise는 내가 정의하지 않은 루트 경로로 리디렉션을 시도합니다.


답변

(이것은 제안 된 편집 으로 게시 되었지만 그 자체의 답변이어야했습니다. 그게 말이되는지 모르겠습니다. 익명의 편집자에게 :이 답변을 자신의 것으로 다시 게시하고 의견을 남겨주세요. 이 사본을 삭제하겠습니다.)

root :to => redirect("/users/login")


답변

@VvDPzZ 답변과 함께 작동합니다. 하지만 약간 수정해야 했어요

  devise_scope :business_owner do
    authenticated  do
      root to: 'pages#dashboard'
    end

    unauthenticated do
      root to: 'devise/sessions#new', as: 'unauthenticated_root'
    end
  end

to:루트 경로 선언 에 광고 를 해야했습니다 . 나는 또한 링크에서 as: :authenticated_root참조 root_path하는 내 응용 프로그램에 이미 일부 위치가 있기 때문에 제거했습니다 . as: :authenticated_root부분을 생략함으로써 기존 링크를 변경할 필요가 없었습니다.


답변

사용자 역할이 다른 것 같습니다. 사용자 리소스에 다음과 같은 범위를 추가해야하는 경우 :

  devise_scope :user do
    get "/logout" => "devise/sessions#destroy"
  end

여기에서 devise 경로 재정의에 대한 자세한 내용을 읽을 수 있습니다.
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes


답변

이러한 솔루션 중 일부는 너무 복잡합니다. Rails를 사용하십시오.

'get' 'users/root', to: 'users#root'config / routes.rb에 추가하십시오 .

UsersController에서 다음과 같이하십시오.

def root
  if user_signed_in?
    redirect_to root_for_signed_in_user_path (or whatever)
  else
    redirect_to new_user_session_path
  end
end