2017-06-24 25 views
0

我安裝了一個faker gem(我確定它不在dev/test裏面!)。我想在視圖上生成幾個隨機引號。我想把它放在welcome/index裏面。如何在Rails視圖中使用faker gem?

裏面我welcome_controller,我有:

require 'faker' 

class WelcomeController < ApplicationController 
    def index 
    @faker_array = [] 
    5.times do |n| 
     @faker_array.push(Faker::HitchhikersGuideToTheGalaxy.quote) 
    end 
    end 

我打過電話,但它給了我"#<NameError: uninitialized constant Faker...錯誤。

<% @faker_array.each do |el| %> 
    <li><%= el %></li> 
    <% end %> 

我也嘗試創建一個名爲list_generator.rb

require 'faker' 

class ListGenerator 
    def faker_hitchhiker_quote 
    return Faker::HitchhikersGuideToTheGalaxy.quote 
    end 
end 

服務並稱爲歡迎索引視圖裏面的服務,像:

<ul> 
    <% 5.times do |el| %> 
    <li><%= ListGenerator.new.faker_hitchhiker_quote %></li> 
    <% end %> 
</ul> 

這樣做仍然給我#<NameError: uninitialized constant Faker::...錯誤。

有沒有一種方法可以將Faker gem整合到我的視圖中以生成隨機引號?

+0

看起來它應該工作,你添加和捆綁faker後重新啓動服務器嗎? – treiff

回答

0

根據自述:

注意:如果你得到一個未初始化的常量法克爾:: [some_class]錯誤,你的寶石的版本是一個記錄在這裏的後面。爲了確保你的寶石是一個記錄在這裏,在你的Gemfile行更改爲:

gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master' 

試試這個辦法。

相關問題