繁体   English   中英

Ruby / Rails-设计-before_action:authenticate_user! 阻止else语句显示

[英]Ruby / Rails - Devise - before_action :authenticate_user! blocking else statement display

我想将我的“计划”利用到一个单独的部分或一个新的文件/位置中。 此页面或部分页面应通过登录页面上的按钮进行访问。 当前,如果用户未登录,我的“计划” 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

您可以有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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM