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整合到我的視圖中以生成隨機引號?
看起來它應該工作,你添加和捆綁faker後重新啓動服務器嗎? – treiff