根据几个人的建议,我被告知要使我的控制器尽可能简洁。目前我的控制器中有这个:
class SearchesController < ApplicationController
def index
raw_result = params[:search] ||= "man on fire"
result = raw_result.gsub(/\s+/, "+")
movie_details = HTTParty.get("http://imdbapi.org/?title="+result+"&type=json")
@searches = ActiveSupport::JSON.decode(movie_details)
end
end
如何在我的模型中将它变成一个新类并在控制器中再次调用它?
当前型号和控制器:
class IMDBSearcher
def self.search(search)
raw_result = search || "man on fire"
result = raw_result.gsub(/\s+/, "+")
movie_details = HTTParty.get("http://imdbapi.org/?title="+result+"&type=json")
ActiveSupport::JSON.decode(movie_details)
end
end
class SearchesController < ApplicationController
def index
@searches = IMDBSearcher.search(params[:search])
end
end