Ruby 看不到瘦文件

问题描述

我正在尝试进行规范测试,场景如下:

   scenario 'Authenticated user creates question' do
    User.create!(email: 'user@test.com',password: '1234567')

    visit new_user_session_path
    fill_in 'Email',with: 'user@test.com'
    fill_in 'Password',with: '1234567'
    click_on 'Log in'

    visit questions_path
    click_on 'Ask question'
    fill_in 'Title',with: 'Test question'
    fill_in 'Body',with: 'text text'
    click_on 'Create'

    expect(page).to have_content 'Your question successfully created.'
  end

  scenario 'Non-authenticated user tries to create question' do
    visit questions_path
    click_on 'Ask question'

    expect(page).to have_content 'You need to sign in or sign up before continuing.'
  end

end 

它给了我一个错误:“QuestionsController#index 缺少请求格式的模板:text/html”。 问题是我有一个索引文件,但格式很薄:index.html.slim 如果我制作索引文件 erb,它会给我另一个错误:“无法找到链接或按钮“提问”“。 所以我认为问题是我的代码看不到瘦文件。在 application.rb 中我已经有了这个代码

 config.generators do |g|
      g.template_engine = :slim
    end 

这是我的 gemfile 的一部分:

group :development do
  gem 'slim-rails'
end

可能是什么问题?为什么 ruby​​ 看不到我的瘦文件

解决方法

问题是您在 Gemfile 的开发组中有 slim-rails 并且您正在运行测试环境。我无法想象您实际上希望每个环境使用不同的模板引擎的场景。

只需将其移出 development 组即可。

gem 'slim-rails','~> 3.2'

如果您不希望您的应用突然中断,也建议为此任务关键的内容添加版本约束。