小编Chi*_*fic的帖子

RSpec:如何存根继承方法current_user(没有设计)?

我有一个基于MHartl的RoR4教程的控制器

就像MHartl一样,我没有使用Devise,我推出了自己的身份验证系统

由于UsersController#Edit视图有一个调用current_user.admin?和控制器调用,因此RSpec出现问题@path_switch = path_switch

我不断得到RSpec错误:

1) User Pages Edit 
 Failure/Error: visit edit_user_path(user)
 NoMethodError:
   undefined method `admin?' for nil:NilClass
 # ./app/controllers/users_controller.rb:106:in `path_switch'
 # ./app/controllers/users_controller.rb:53:in `edit'
 # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

UsersController:

class UsersController < ApplicationController
  ...
  def edit
    @user = User.find(params[:id])
    @path_switch ||= path_switch                        #error
  end
  ...
  def path_switch
    if current_user.admin?                               #error
      users_path
    else
      root_path
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我发现这篇非常有用的文章让我希望我能走上正轨,但我无法让它发挥作用.

据我所知(更新):

user_pages_spec.rb:

require 'spec_helper'
require 'support/utilities' …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails stub ruby-on-rails-4

23
推荐指数
2
解决办法
1万
查看次数

Rails 6 Zeitwerk“弃用警告:初始化自动加载常量...”,但我不知道在哪里?

我发誓我已经阅读了文档,并且我认为我对这些原理有了基本的了解,但是我一生都无法弄清楚我在哪里不恰当地加载了这些常量。

我正在升级一个最初是 Rails 5.2 的应用程序

当我运行 RSpec、服务器、本地控制台等时,我收到此警告。

DEPRECATION WARNING: Initialization autoloaded the constants ApplicationHelper, EventsHelper, FontAwesome::Rails::IconHelper, DeviseHelper, ErrorHandler, and ApplicationController.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.
Run Code Online (Sandbox Code Playgroud)

以下是不正确自动加载的常量:

  • ApplicationHelper - 标准 Rails 文件
  • EventsHelper - EventsController 的标准 Rails 文件
  • FontAwesome::Rails::IconHelper -来自 gem,不在我的应用程序目录中
  • DeviseHelper -来自 gem,不在我的应用程序目录中
  • ErrorHandler -位于“app/controllers/concerns/error_handler”
  • ApplicationController -标准 Rails 文件

我寻找过可能调用这些常量的实例includerequire但没有找到。我特别关注我的初始化程序。

我已经运行(并通读)bin/rails zeitwerk:check没有任何明显的提示。我看到这些实例按预期加载:

... …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-6 zeitwerk

13
推荐指数
1
解决办法
4689
查看次数

带有 url 查询参数选项的 Rails 5 redirect_back

根据文档

redirect_back(fallback_location:,allow_other_host:true,**args)

  • :fallback_location- 将用于丢失Referer标头的默认后备位置。

  • :allow_other_host - 允许或禁止重定向到与当前主机不同的主机,默认为 true。

  • 可以传递给 redirect_to 的所有其他选项都被接受为选项并且行为是相同的。

    redirect_to允许我通过将它们作为哈希传递来将参数添加到 url

    那么,为什么这些都不适合我:

  • redirect_back fallback_location: tasks_path, allow_other_host: false, syncing: true
  • redirect_back fallback_location: tasks_path, allow_other_host: false, { syncing: true }
  • redirect_back fallback_location: tasks_path, allow_other_host: false, options: { syncing: true }
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, options: { syncing: true })
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, syncing: true)
  • ...以及我能想到的上述任何其他迭代。

    所有这些(都是有效的代码),只需返回我而不添加参数

    我正在尝试实现此 URL: (back_url or fallback_location) + '?syncing=true'

    ruby-on-rails-5

    4
    推荐指数
    1
    解决办法
    2243
    查看次数

    比"!current_user.nil?"更漂亮的Ruby?

    好吧,我是绿色的,但是下面的def似乎是双负的

    def signed_in?
      !current_user.nil?  #the current user is not...um...not
    end
    
    Run Code Online (Sandbox Code Playgroud)

    由于我的病人导师M.Hartl在他的Rails教程中使用它.我必须相信这是吱吱作响,但......

    不会说"当前用户"更干净吗?

    def signed_in?
      current_user
      current_user.present?
      current_user.any?
      !!current_user
    end
    
    Run Code Online (Sandbox Code Playgroud)

    爆炸有什么好处?

    ruby

    2
    推荐指数
    1
    解决办法
    118
    查看次数

    Ruby strftime:如果为零则不显示分钟

    我正在尝试实现这种风格的输出:

    Time.new(2021,9,19,6,0,0) => "6am"
    Time.new(2021,9,19,6,30,0) => "6:30am"
    
    Run Code Online (Sandbox Code Playgroud)

    我得到的最接近的是strftime('%l:%-M%P')

    Time.new(2021,9,19,6,0,0).strftime('%-l:%-M%P') => "6:0am"
    Time.new(2021,9,19,6,30,0).strftime('%-l:%-M%P') => "6:30am"
    
    Run Code Online (Sandbox Code Playgroud)

    是否有已知的方法来抑制%M%-M当为零时,或者我需要在这里做我自己的事情?

    ruby strftime

    2
    推荐指数
    1
    解决办法
    165
    查看次数

    M Hartl的Ruby on Rails教程第5章主页上的自定义标题

    在Michael Hartl的RoR教程中:第5章末尾的练习涉及简化RSpec测试.

    5.37(http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-layout_exercises)定义了主页的几个测试.在spec/helpers/application_helper_spec.rb文件中:

    require 'spec_helper'
    
    describe ApplicationHelper do
    
      describe "full_title" do
      .
      .
      .
      .
        it "should not include a bar for the home page" do
          full_title("").should_not =~ /\|/
        end
      end
    end
    
    Run Code Online (Sandbox Code Playgroud)

    为了通过这个测试,我需要显示主页标题:"Ruby on Rails Tutorial Sample App"而不是"Ruby on Rails Tutorial Sample App | Home"

    但是,本教程并未指导我如何编写更改代码.

    这是我尝试过的:

    在app/views/static_pages/home.html.erb中:

    1. 去掉:

      <% provide(:title, 'Home') %>

    在app/views/layouts/application.html.erb中将标记编辑为:

    <title>
     <% if :title
      full_title = "Ruby on Rails Tutorial Sample App | " && yield(:title)
     else
      full_title = "Ruby on Rails Tutorial Sample …
    Run Code Online (Sandbox Code Playgroud)

    ruby ruby-on-rails

    1
    推荐指数
    1
    解决办法
    1163
    查看次数