未定义的方法`hello' 请帮助,我只是想让它在视图中显示字符串似乎无法绕过这个?我在错误的文件中吗?
karlsgem.rb
require "karlsgem/version"
module Karlsgem
class KarlsGeming
def hello
puts "Hello World"
end
end
end
index.html.erb
<% if notice %>
<p id="notice" ><%= notice %></p>
<% end %>
<%= @kgem %>
<h1>Your Drinks Catalog</h1>
<% @products.each do |product| %>
<div class="entry" >
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%=sanitize product.description %>
<div class="price_line" >
<span class="price" >€<%= product.price %></span>
<%= button_to 'Add to Cart' , line_items_path(:product_id => product) %>
</div>
</div>
<% end %>
store_controller.rb
require 'karlsgem'
class StoreController < ApplicationController
def index
@products = Product.all
@cart = current_cart
@kgem = Karlsgem::KarlsGeming.hello
end
end
答案 0 :(得分:0)
您将hello定义为实例方法。为了Karlsgem::KarlsGeming.hello
,您必须将其定义为类方法:
require "karlsgem/version"
module Karlsgem
class KarlsGeming
def self.hello
puts "Hello World"
end
end
end