2014-02-06 46 views
3

在我的Rails應用程序中,有很多頁面不需要您登錄即可查看。例如,某個用戶的配置文件頁面。我當前的會話邏輯重定向回到上一頁,如果該頁面需要您登錄。如果它不是,就像個人檔案頁面一樣,然後點擊登錄並登錄,它會將您帶到指定的網址我的SessionsController行動。這使我走到了十字路口。我需要在每個頁面上都有一個模式(在layouts/application.html.erb中),它具有會話窗體,因此用戶可以單擊登錄,模型將打開,並且登錄時只刷新頁面,以便用戶可以保持當前頁。但是,我現在正在嘗試並發現一些錯誤。另一種選擇是擁有單獨的登錄頁面,但不管您在應用內部來自哪個頁面,它都會將您重定向回。不幸的是,我不確定如何爲此編寫代碼。在每個頁面上登錄表單或重定向到頁面的表單

這裏是我目前正在嘗試通過模態在每個頁面上獲得會話表單的代碼。

**SessionsController #create action** 

def create 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
    sign_in user 
    redirect_back_or request.original_url 
    else 
    flash.now[:error] = "Invalid email/password combination." 
    render 'new' 
    end 
end 

**layouts/application.html.erb** 

<% unless signed_in? %> 
...modal code 
    <%= form_for(:session, url: sessions_path) %> 
     <%= text_field ... %> 
     <%= password_field .... %> 
     <%= submit button %> 
    <% end %> 
... # I've shorted the code here obviously 
<% end %> 

此表工作正常的/login頁:match '/login', to: "sessions#new", via: "get"

然而,當我使用在其他頁面這種形式,我得到這個錯誤:

`Missing template public/error with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. ` 

這頁轉到它:

http://localhost:3000/sessions 

我該如何使這個會議形式工作在每一個p網站的年齡?由於

更新

用它打的多後,我意識到,形式,當我在SessionController更改代碼以

redirect_back_or profile_path(user) 

,而不是當前

redirect_back_or request.original_url 
工作

我該如何重定向回當前頁面,而不是像配置文件頁面那樣指定特定頁面?

回答

0

我想我找到了答案。如果您有更好的解決方案,請仍然回答。

redirect_back_or :back 
+0

確保你redirect_back_or:背對背! – DiegoSalazar