0

在尝试创建登录页面的测试中,我不断遇到同样的两次失败。

这是错误消息: $ bundle exec rspec spec/requests/user_pages_spec.rb FF

失败:

1) 用户页面注册页面Failure/Error: before { visit signup_path } ActionView::Template::Error: undefined method |' for "Ruby on Rails Tutorial Sample App":String # ./app/helpers/application_helper.rb:9:infull_title' # ./app/views/layouts/application.html.erb:4:in _app_views_layouts_application_html_erb__2148911516627374684_2168968760' # ./spec/requests/user_pages_spec.rb:8:inblock (3 levels)在 '

2) 用户页面注册页面Failure/Error: before { visit signup_path } ActionView::Template::Error: undefined method |' for "Ruby on Rails Tutorial Sample App":String # ./app/helpers/application_helper.rb:9:infull_title' # ./app/views/layouts/application.html.erb:4:in _app_views_layouts_application_html_erb__2148911516627374684_2168968760' # ./spec/requests/user_pages_spec.rb:8:inblock (3 levels)在 '

在 0.17668 秒内完成 2 个示例,2 个失败

失败的例子:

rspec ./spec/requests/user_pages_spec.rb:10 # 用户页面注册页面 rspec ./spec/requests/user_pages_spec.rb:11 # 用户页面注册页面

这是文件 user_pages_spec.rb

需要'spec_helper'

描述“用户页面”做

主题{页面}

在 { 访问 signup_path } 之前描述“注册页面”

it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }

结束结束

这是文件 application_helper.rb:

模块 ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      "#{base_title}" | "#{page_title}"
    end
  end
end

这是文件 routes.rb SampleApp::Application.routes.draw do get "users/new"

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

我一直坚持这一点,所以任何帮助将不胜感激!

谢谢!

4

2 回答 2

0

它在此处为您提供的 rails 错误消息非常具有描述性。

如果我们看这条线

undefined method `|' for "Ruby on Rails Tutorial Sample App":String # ./app/helpers/application_helper.rb:9:in full_title

它告诉我们它在方法定义中的 application_helper.rb 的第 9 行找不到|为字符串调用的方法。如果我们到达那条线,我们可以看到"Ruby on Rails Tutorial Sample App"full_title

 "#{base_title}" | "#{page_title}"

哪个 ruby​​ 解释为“运行方法 | 在“#{base_title}”的结果上(在这种情况下计算为字符串“Ruby on Rails 教程示例应用程序”),参数为“#{page_title}”。因为字符串不'没有'|' 方法,它返回一个“未定义的方法”错误。

要修复,只需将行更改为

 "#{base_title} | #{page_title}"
于 2012-06-05T22:49:08.853 回答
0

这一行:

     "#{base_title}" | "#{page_title}"

是罪魁祸首。

我将为您分解错误消息(您会想要擅长阅读这些内容):

 ActionView::Template::Error: undefined method |' for "Ruby on Rails Tutorial Sample App":String # 

这说明 ActionView 模板系统在运行您的帮助程序时遇到了问题。具体来说,它说你试图调用一个名为“|”的未定义方法 (方法的名称是管道字符)在 String 类的对象上。

如果您查看 String 类的文档,在这里,您会看到方法 '|' 不在可用方法列表中。

我的猜测是您正试图将它们连接在一起并包含管道字符,例如面包屑。在这种情况下,您只需将整行括在引号中,如下所示:

     "#{base_title} | #{page_title}"

祝你好运!

于 2012-06-05T22:53:11.020 回答