2012-08-23 14 views
0

我是相當新的軌道,並希望保持的URL相同的登錄用戶,如果有錯誤,「新的」模板呈現軌道3路由保持個性網址後

這裏是我的路線

resources :users, only: [:new, :create] 
resources :sessions, only: [:new, :create, :destroy] 

root to: 'pages#home' 

match '/signin', to: 'sessions#new' 
#match '/signin', to: 'sessions#create', via: :post, as: :post_session 
match '/logout', to: 'sessions#destroy' 

,這裏是會話控制器代碼

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_url 
    else 
     flash.now[:error] = 'Invalid email or password' 
     render 'new' 
    end 
end 

,你可以看到,我有註釋掉抓後,使渲染「新」的呼叫保持/登入自定義路線網址,但當我這樣做時,錯誤的閃光消息不會在頁面中呈現(儘管沒有該路線)。我試圖使用閃光燈沒有現在的方法,仍然沒有看到我的消息出現。有任何想法嗎?

編輯:

我試了下面的建議,仍然看到這個問題。在查看訪問日誌後,應用程序被路由到第一個登錄路由,因爲它是用匹配定義的而不是獲取的。 我的更新和工作路線文件現在看起來是這樣

resources :users, only: [:new, :create] 
#resources :sessions, only: [:new, :create, :destroy] 

root to: 'pages#home' 

match '/signin', to: 'sessions#new', via: :get 
match '/signin', to: 'sessions#create', via: :post, as: :post_session 
match '/logout', to: 'sessions#destroy', via: :delete 

回答

0

取出現在,應該沒有必要。你的路線可能相互衝突。根據您如何設置比賽線路,您可以完全刪除resources :sessions,然後取消匹配線的註釋。這應該照顧你所需要的。

此外,請務必回到您的其他問題,並接受一些答案。 0%的接受率不可能吸引答案。

編輯

根據您的意見,它可能只是不知道在這個時間來渲染什麼,當你刪除的資源調用。嘗試更改爲:

render "sessions/new" 
+0

現在我已經接受了一些答案;)然而,閃存的消息仍與這條新航線的文件資源消失:用戶,只有:[:新:創建] #resources:會話,只:[:new,:create,:destroy] root to:'pages#home' match'/ signin',to:'sessions#new' match'/ signin',to:'sessions#create' ,通過:post,作爲:: post_session match'/ logout',to:'sessions#destroy',via :: delete – Austin

+0

可能是一個愚蠢的問題,但是你的視圖或佈局輸出'flash [:error]' ? – agmcleod

+0

是的,視圖輸出所有的Flash消息。 Flash消息使用資源:會話路由工作,但隨後URL被錯誤地重寫爲/ sessions。與我的自定義路由匹配登錄後,閃存郵件丟失 – Austin