【问题标题】:Ruby / Rails - Devise - before_action :authenticate_user! blocking else statement displayRuby / Rails - 设计 - before_action :authenticate_user!阻塞 else 语句显示
【发布时间】:2016-10-01 04:01:29
【问题描述】:

我想将我的“计划”应用到一个单独的部分或者完全是一个新的文件/位置。此页面或部分页面应通过登录页面上的按钮访问。 目前,如果用户未登录,我的“计划”html 设置为有条件地显示在我的主页上。由于“before_action :authenticate_user!”我在主页上,查看“else”(里面有图)不能查看。我应该将计划分开到单独的页面上并删除条件语句吗?或者我应该把它分出来并在我的 else 语句中呈现视图?

我需要提供其他信息吗?

我是否使用了正确的术语?


home.html.erb

<div class="row">
 <% if user_signed_in? %>

    <%= render 'users/home' %>

 <% else %>

 <!--Plans-->
 <div class="row">
    <div class="col-lg-3">
        <div class="well text-center">

            <!--Plan #1-->
            <%= link_to "REGISTER", new_user_registration_path(plan: @contributor_plan.id), class:'btn btn-info btn-block' %>

            <!--Plan #2-->
            <%= link_to "REGISTER", new_user_registration_path(plan: @elitecontributor_plan.id), class:'btn btn-warning btn-block' %>

            <!--Plan #3-->
            <%= link_to "REGISTER", new_user_registration_path(plan: @technician_plan.id), class:'btn btn-info btn-block' %>

            <!--Plan #4-->
            <%= link_to "REGISTER", new_user_registration_path(plan: @elitetechnician_plan.id), class:'btn btn-info btn-block' %>

            </div>
        </div>
    </div>

 <% end %>

pages_controller.rb

class PagesController < ApplicationController
before_action :authenticate_user!, except:  [:landing, :plans]

def landing

end

def plans
end

def home
    @contributor_plan = Plan.find(1)
    @elitecontributor_plan = Plan.find(2)
    @technician_plan = Plan.find(3)
    @elitetechnician_plan = Plan.find(4)
    @center_plan = Plan.find(5)
    @elitecenter_plan = Plan.find(6)
    @affair_plan = Plan.find(7)
    @eliteaffair_plan = Plan.find(8)
end

【问题讨论】:

    标签: ruby-on-rails ruby if-statement devise conditional


    【解决方案1】:

    您可以有 2 个单独的路由路由,一个用于经过身份验证的用户,一个用于未经过身份验证的用户。我假设您使用的是 Devise?

    配置/路由:

    authenticated do
      root :to => 'pages#home', as: :authenticated
    end
    
    root :to => 'pages#plans'
    

    要回答您问题的第二部分,是的,您可以将计划分成自己的资源,我建议您这样做。

    那么它会是这样的:

    配置/路由:

    authenticated do
      root :to => 'home#dashboard', as: :authenticated
    end
    
    root :to => 'plans#index'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      相关资源
      最近更新 更多