【问题标题】:Ruby / Rails 4.2.5 - Devise - before_action :authenticate_user - User plans page lockedRuby / Rails 4.2.5 - 设计 - before_action :authenticate_user - 用户计划页面锁定
【发布时间】:2016-11-21 02:30:10
【问题描述】:

我在除登陆之外的页面上使用 before_action 实现了设计身份验证。唯一的问题是,这现在将我锁定在用户计划页面之外。我可以很容易地允许计划页面作为一个例外,除非计划部分显示在另一个主页上,以便在有人未登录时显示。我尝试允许主页作为例外,但收到控制器错误(PagesController#home 中的 ActiveRecord::RecordNotFound)。我的问题是 1.) 我应该想办法让 else 显示并保持局部化,还是应该重新组织代码并将它们全部分开?

我是否需要包含其他文件(路由?)

另外,如果我的问题格式不正确,请随时纠正我。谢谢。

home.html.erb

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

      <%= render 'users/home' %>

  <% else %>

      <%= render 'users/plans' %>        

  <% end %>
</div>

pages_controller.rb

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

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

end

控制器错误(当 home 被排除在身份验证之外时)

【问题讨论】:

  • 您的数据库中是否有计划记录。如果没有,请尝试通过10.times { Plan.create } 在您的控制台中创建一些。

标签: ruby-on-rails ruby devise


【解决方案1】:

flash 信息非常明显:

"RecordNotFound, Couldn't find Plan with 'id'=1.

请创建至少 8 个 ID 从 1 到 8 的计划:

8.times do |i|
 Plan.create(...., id: i+1)
end

【讨论】:

  • 我在不知不觉中删除了我的数据库。我曾经有过这些计划记录,因此我对为什么它们不再存在感到困惑。一旦我确认数据库是空的,我就通过 rails 控制台手动重新添加了我的计划
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多