0

Karlsgem::KarlsGeming:Class 的未定义方法“你好”

请帮忙,我只是希望它在视图中显示字符串,似乎无法解决这个问题?我在错误的文件中吗?

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" >€&lt;%= 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
4

1 回答 1

0

您将 hello 定义为实例方法。为此,Karlsgem::KarlsGeming.hello您必须将其定义为类方法:

require "karlsgem/version"
module Karlsgem
  class KarlsGeming
    def self.hello
        puts "Hello World"
    end
  end
end
于 2013-04-05T17:20:04.787 回答