Search code examples
ruby-on-railsrubyrake

Rails: How to define a function in a rake task


In my task file post.rake, I want to reuse a function

  def save_post(title, href, source)
    post = Post.new(title: title, url: href, source: source)
    if post.save
      puts title + 'saved'
    else
      puts title + 'not saved'
    end
  end

However, when I define it in this file and re-use it, it returns

NoMethodError: undefined method `save_post' for main:Object

The post.rake looks like this:

task :fetch_post => :environment do
  require 'nokogiri'
  require 'open-uri'

  url = 'http://example.com'
  doc = Nokogiri::HTML(open(url) )
  puts doc.css("title").text
  doc.css(".a").each do |item_info|
    title = item_info.text
    href = item_info['href']
    save_post(title, href)
  end

  def save_post(title, href)
    post = Post.new(title: title, url: href)
    if post.save
      puts title + 'saved'
    else
      puts title + 'not saved'
    end
  end
end

The content-scraping part works. I just move the post-saving code out, wanting to abstract the method out.

Where should I put the def method?


Solution

  • OOOH~~~,function postion is wrong, like this, it works:

    task :fetch_post => :environment do
      require 'nokogiri'
      require 'open-uri'
    
      def save_post(title, href)
        post = Post.new(title: title, url: href)
        if post.save
          puts title + 'saved'
        else
          puts title + 'not saved'
        end
      end
    
      url = 'http://example.com'
      doc = Nokogiri::HTML(open(url) )
      puts doc.css("title").text
      doc.css(".a").each do |item_info|
        title = item_info.text
        href = item_info['href']
        save_post(title, href)
      end
    end