0

I am trying to use open method of open-uri gem in Rails.

When I try it in standalone Ruby application I just require it on top of the file and everything is ok. But now I want it to use it controller in Rails app. Is it advisable to put require on top of controller?

Controller code:

def search
  if params.has_key? :q
    params[:q] =~ /\?v=(.*?)&/
    xmlfeed = Nokogiri::XML(open("http://gdata.youtube.com/feeds/api/videos?q=#{$1}"))
    if xmlfeed.at_xpath("//openSearch:totalResults").content.to_i == 1
      flash[:notification] = "Video #{xmlfeed} found"
      #save video id to database
      redirect_to root_url
    else
      flash[:error] = "Video #{xmlfeed} not found"
      redirect_to root_url
    end
  else
    flash[:error] = "You didn't insert video URL."
    redirect_to root_url
  end
end

Error:

No such file or directory - http://gdata.youtube.com/feeds/api/videos?q=cSnkWzZ7ZAA

Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93

2 Answers2

1

I think that a better practice is to create a model for fetching youtube feeds and put all code that fetches the videos and parses the result in there - the controller should be as thin as possible.

EfratBlaier
  • 559
  • 2
  • 6
1

You should make a model for this kinda stuff.

But answering directly to you question. You will not really feel any difference when putting it at the top of the controller or in the action you need it.

Maybe high intensive apps might notice it when benchmarking but I haven't seen it yet.

Yavor Ivanov
  • 449
  • 3
  • 8