【发布时间】:2016-07-25 19:02:21
【问题描述】:
我正在测试 RoR,目前正在尝试找出允许具有有效会话的用户跳过登录的功能。
登录按钮通常会提示您进入登录页面,而我想让有会话的人跳过它并使用条件语句立即重定向到显示视图。 (通过 current_user 检查) (如果您需要任何其他代码,只需评论)
这是登录视图
<%= form_for :session, url: '/login' do |s| %>
<%= s.text_field :username, :placeholder => 'Username' %>
<%= s.password_field :password, :placeholder => 'Password' %>
<%= s.submit "Login" %>
<% if current_user %>
<!-- What do i write here? -->
<!-- Maybe I should put this in a controller? -->
<% end %>
<% if flash[:notice] && current_user %>
<div class="notice"><%= flash[:notice] %></div>
<% end %>
这是包含 current_user 函数的应用程序控制器。 (以防万一)
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
def current_user
@current_user ||= User.find_by(id: session[:id]) if session[:id]
end
end
【问题讨论】:
-
我建议退后一步,研究 MVC 原则(谷歌搜索)。这个重定向应该放在控制器中,而不是视图中。
标签: ruby-on-rails html view