Search code examples
ruby-on-railsrubygemsmediawiki-apiwikimedia

Struggling with Rails Wikiquote gem?


I am writing my first rails website and have come across a problem. I want to show a "quote of the day" on the welcome page using the 'wikiquote' gem (http://hemanth.github.io/wikiquote-gem/). I put it in the code below and thought it would work, but was mistaken. The browser does not say there are any errors, but nothing shows up either. Any thoughts? Am I doing this utterly wrong?

in welcome_controller.rb

class WelcomeController < ApplicationController

  def index
  end    

  def get_qod
    @qod = WikiQuote.get
  end
end

in welcome/index.html.erb

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


Solution

  • Yes. You're doing it wrong.

    if you want @qod to be available for index, you need to run it inside index.

    class WelcomeController < ApplicationController
    
      def index
        @qod = WikiQuote.get
      end    
    
    end
    

    Alternatively you can outsource this method:

    class WelcomeController < ApplicationController
    
      before_action :get_quote, only: [:index]
    
      def index       
      end
    
      private 
        def get_quote
          @qod = WikiQuote.get
        end
    
    end