2013-03-17 4 views
0

최근에/signin 및/signup 경로를 사용하여 새 홈페이지 (index.html)를 내 레일 앱 (폴더 public/index.html에 있음)에 구현했습니다. 내 웹 사이트에서 index.html을 구현하기 전에 로그인 또는 가입 한 후 root_url (home.html.erb)으로 사용자를 리디렉션했습니다. 로그 아웃하면 root_path로 리디렉션했습니다.로그인하거나 가입 한 후 index.html로 리디렉션되는 것을 방지하는 방법은 무엇입니까?

이 새로운 index.html 이후에 사용자가 로그인하거나 등록하려고하면 index.html로 다시 연결됩니다. 많은 코드를 수정하지 않아도 원래 root_url에 성공적으로 로그인 할 수 있습니까?

class SessionsController < ApplicationController 

    def new 
    end 

    def create 
    user = User.find_by_email(params[:session][:email]) 
    if user && user.authenticate(params[:session][:password]) 
     sign_in user 
     redirect_to root_path 
    else 
     flash.now[:error] = "Invalid email/password combination" 
     render 'new' 
    end 
    end 

    def destroy 
    sign_out 
    redirect_to root_path 
    end 
end 

UsersController

def create 
    @user = User.new(params[:user]) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome!" 
     redirect_to root_path 
    else 
     render 'new' 
    end 
    end 

이가 내 routes.rb에서 컨트롤러 명을 처리하는 컨트롤러의 이름이어야합니다 home_ControllerName_pathroot_path을 변경하는

root to: 'static_pages#home' 
+0

session_controller 또는 users_controller에서 login/sign_up 조치를 찾으십시오. 그러면 둘 다 끝에 리디렉션 조치가 적용됩니다. redirect_to '무엇이든지'대신 redirect_to root_path로 변경하십시오. – Zippie

+0

두 컨트롤러에서 def create 명령으로 root_path로 변경했지만 여전히 동일한 문제가 발생합니다. – johbones

+0

업데이트 된 코드에 코드를 게시했습니다. rake route를 실행하고 home.html.erb이 실제로 root_path인지 확인할 수 있습니까? – Zippie

답변

0

시도가 무엇인가 홈 액션.

+0

그는 이미 그것을하고있다. 컨트롤러의 이름은'static_pages'입니다 – Zippie

+0

@ johbones 코드에 기반하여'home_static_page_path'이어야합니다 – thesubroot

+0

컨트롤러에서 'redirect_to home_static_page_path'를 시도했지만이 에러가 발생했습니다 : 정의되지 않은 지역 변수 또는 메소드의 home_static_page_path가 # johbones

1

우선 (foo_urlfoo_path 내지 일반) root_urlroot_path 유일한 차이점은 후자 반면, 경로 (호스트 후의 비트) 막 (즉 http://example.com/...) 전 전체 URL이 있다는 것이다. 간단한 리디렉션의 경우 동일한 결과가 나타납니다.

public/index.html이 있으면 '/'방문 (즉 root_path)이됩니다.

가입 후 다른 페이지로 사용자를 보내려면 리디렉션을 변경하십시오. 예를 들어 당신의 루트 파일은 홈 컨트롤러의 index 액션에 사람을 보낼 것 home_path로 리디렉션

get '/home' => 'home#index', :as => :home 

이 있다면.

1

public/index.htmlroot_path 레일 경로보다 우선합니다.

공용 디렉토리에 index.html이라는 파일이 있으면 root_path에 액세스 할 수 없습니다.

당신은 당신의 root_path

EDIT 이외의 다른 경로를 다른 것으로 index.html의 이름을 바꾸거나 사용할 필요가 :

또 다른 옵션은 root_path에 대한 두 개의 서로 다른 ERB 템플릿을하는 것입니다.그런 다음 root_path에 대한 컨트롤러 액션에, 당신이 할 수 있습니다 :

class StaticPages < ApplicationController 
    def home 
    if user_signed_in? 
     render 'home_signed_in' 
    else 
     render 'home_signed_out' 
    end 
    end 
end 

그런 다음에 두 개의 ERB 템플릿을 만들어야합니다,

/app/views/static_pages/home_signed_in.html.erb

/app/views/static_pages/home_signed_out.html.erb

또한 예제 코드에서 user_signed_in? 메서드를 정의하거나 자신의 메서드를 사용하여 d로 바꿀 필요가 있습니다. etect 사용자는 로그인되고 /public/index.html

+0

index.html의 이름을 다른 것으로 변경하면 누군가가 www.mydomain으로 갈 때이 페이지가 나타나게 할 수 있습니까? .com? – johbones

+0

당신이 할 수 있을지 모르겠다. 당신은'index.html' 또는'root_path' 만 가질 수 있습니다. 두 충돌과 웹 서버는'root_path' 레일을 통해 정적'index.html'을 선택할 것입니다. – Sam

+0

내 대답을 다른 가능한 해결책으로 업데이트했습니다. – Sam

0

변화 다음 당신의 routes.rb 파일로 이동하게 제거하는 것을 잊지 마세요 경우

root :to => 'main#home_page' 

루트를 수정하여 어떤 경로로 리디렉션 할 수 있습니다. 설정에서 /routes.rb 파일

관련 문제