0

我正在编写我的第一个 Rails 网站,但遇到了一个问题。我想使用“wikiquote”gem(http://hemanth.github.io/wikiquote-gem/)在欢迎页面上显示“今日报价”。我把它放在下面的代码中,并认为它会起作用,但是弄错了。浏览器没有说有任何错误,但也没有任何显示。有什么想法吗?我这样做完全错了吗?

welcome_controller.rb

class WelcomeController < ApplicationController

  def index
  end    

  def get_qod
    @qod = WikiQuote.get
  end
end

welcome/index.html.erb

<h3> <%= @qod.to_s %></h3>

4

1 回答 1

1

是的。你这样做是错的。

如果您希望 @qod 可用于索引,则需要在索引中运行它。

class WelcomeController < ApplicationController

  def index
    @qod = WikiQuote.get
  end    

end

或者,您可以外包此方法:

class WelcomeController < ApplicationController

  before_action :get_quote, only: [:index]

  def index       
  end

  private 
    def get_quote
      @qod = WikiQuote.get
    end

end
于 2014-09-09T05:10:31.350 回答