2014-02-06 43 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