1

new to rails and trying to get youtube-g to work... I am having a difficult time passing a users search from the form_tag to the controller.. for example:

  1. user lands on page, enters - tiger videos - in text_field_tag
  2. controller takes - tiger videos - and searches
  3. user lands on another page displaying the results...

  4. user lands on page, enters query

index.html.erb

<%= form_tag(youtube_path) do %> 
    <%= label_tag(:q, "Search for:") %> 
    <%= text_field_tag(:wth) %> 
    <%= submit_tag("Search") %>
    <% end %>

--

  1. controller takes query and searches

youtubecontroller.rb

class YoutubeController < ApplicationController
def search
  require 'youtube_g'
  client = YouTubeG::Client.new
client.videos_by(:query => params[:wth])
end
end

--

my routes file:

ActionController::Routing::Routes.draw do |map|
map.search “search”, :controller => “youtube”

I havn´t got to step 3 yet..

what I want is a page with just a text box and a submit button, and when a user enters text and submits, they are brought to a new page that renders 10 youtube results. any help is much appreciated!!!

http://youtube-g.rubyforge.org/

1 Answer 1

0

search.html.erb

 <%= form_tag(:action => "search") do %> 
      <%= label_tag(:q, "Search for:") %> 
      <%= text_field_tag(:wth) %> 
      <%= submit_tag("Search") %>  
 <% end %>

YoutubeController

require 'youtube_g'
class YoutubeController < ApplicationController
  def index         
  end

  def search  
    if request.post?
     client = YouTubeG::Client.new
     @youtube = client.videos_by(:query => params[:wth],:per_page => 10)         
     render :action => :index
    end 
  end
end

index.html.erb

<% @youtube.videos.each do |video| %>
   <%= video.title %>
<% end %>

routes.rb

 map.resources :youtube, :collection => { :search => :get }
2
  • 1
    HI thank you for your response! I was able to get results showing.. but the form in search.html.erb does not display.. when I navigate to 0.0.0.0:3000/youtube/search rather than a form displaying, it automatically renders search results.. this is what it spits out #, @media_content=[#, @default=true, @url="youtube.com/v/…, @title="X-Men Evolution: Season 3, Episode 3", @published_at=Wed Mar 04 20:45:26 UTC 2009, @duration=1285, @thumbnails=[#, #, #, #], @player_url="youtube.com/watch?v=NkfuPTm1zUo&feature=youtube_gdat...
    – Felix
    Commented Oct 9, 2010 at 1:52
  • 1
    Sorry. I don't test. I have just update my code. please see again. I tested it. It's ok. Hope it'll helpful for you. :)
    – khanh
    Commented Oct 9, 2010 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.