使用 Rails 3.2。我current_page?
用来确定当前页面以添加active
到适当的导航链接。
当登录失败时,它会返回到user_sessions#new
,但current_page?
不再识别login_path
,因此不会将类添加active
到链接中。
我不能使用redirect_to
,因为我想捕捉控制器中的错误。
我怎样才能current_page?
认识到user_sessions#new
呢?
下面是我的代码:
# routes.rb
# Following 2 lines ensure that the login url is maintained as "login" even there are errors after submitting the form
match 'login' => 'user_sessions#new', :as => :login, :via => :get
match 'login' => 'user_sessions#create', :as => :login, :via => :post
# user_sessions_controller.rb
class UserSessionsController < ApplicationController
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:success] = "Login successful!"
redirect_back_or_default profile_account_url
else
render 'new'
end
end
end
# _sub_nav.html.erb
<ul class="nav nav-tabs">
<li<%= " class=active" if current_page?(login_path) %>>
<a href="<%= login_path %>">Login</a>
</li>
<li<%= " class=active" if current_page?(signup_path) %>>
<a href="<%= signup_path %>">Signup</a>
</li>
</ul>