views:

69

answers:

1

Hi all,

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/

A: 

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 }
Nam Khanh
Felix
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. :)
Nam Khanh