Ruby on rails 使用空对象模式设计的来宾用户

Ruby on rails 使用空对象模式设计的来宾用户,ruby-on-rails,ruby,authentication,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Authentication,Ruby On Rails 4,Devise,我试图在Desive中实现一个Guest用户,但是这样做user\u signed\u in?总是计算为true我该如何解决这个问题 我不能使用登录?,因为我有管理员和用户,所以如果我在管理面板上登录,然后转到主网站,如果我使用登录? 这是密码 应用程序控制器 def current_user if devise_controller? @current_user = super else @current_user ||= super || Guest.new en

我试图在Desive中实现一个
Guest
用户,但是这样做
user\u signed\u in?
总是计算为
true
我该如何解决这个问题

我不能使用
登录?
,因为我有
管理员和
用户
,所以如果我在管理面板上登录,然后转到主网站,如果我使用
登录?

这是密码

应用程序控制器

def current_user
  if devise_controller?
    @current_user = super
  else
    @current_user ||= super || Guest.new
  end
end
def current_user
  @current_user ||= super || Guest.new
end

def user_signed_in?
  current_user.is_a? User
end
查看

<p>Welcome <strong><%= current_user.first_name %></strong></p>
  <ul>
    <% if user_signed_in? %>
      <li>
        <%= link_to 'Sign out', destroy_user_session_path,
          method: :delete %>
      </li>
     <% else %>
      <li><%= link_to 'Sign up', new_user_registration_path %></li>
      <li><%= link_to 'Sign in', new_user_session_path %></li>
     <% end %>
  </ul>

Deviate在此处定义
用户\u登录?

      def self.define_helpers(mapping) #:nodoc:
        mapping = mapping.name

        class_eval <<-METHODS, __FILE__, __LINE__ + 1
          def authenticate_#{mapping}!(opts={})
            opts[:scope] = :#{mapping}
            warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
          end

          def #{mapping}_signed_in?
            !!current_#{mapping}
          end

          def current_#{mapping}
            @current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
          end

          def #{mapping}_session
            current_#{mapping} && warden.session(:#{mapping})
          end
        METHODS

        ActiveSupport.on_load(:action_controller) do
          if respond_to?(:helper_method)
            helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
          end
        end
      end

你的策略进展如何?这对你有用吗?是的,它还没有坏:)酷,好东西。我自己实现了它,唯一的区别是我当前的用户方法是这样的:
super | | | GuestUser.new
我最近遇到了一个类似的问题,并根据不同的方法解决了它。在您的情况下,
user\u signed\u In?
将被定义为当前用户。signed\u In?
,而
signed\u In?
方法将在
user
Guest
中定义,分别返回
true
false
。根据Sandi的谈话,这样做的好处是,您不再拥有行为,而是依靠“真实”和空对象对同一消息做出不同的响应。
class Guest
  def !
    true
  end
end