2010-10-07 32 views
1

新軌道,並試圖讓YouTube-G的工作...我有傳遞用戶困難的時候,從搜索的form_tag到控制器。例如:如何使用youtube-g紅寶石寶石?

  1. 用戶土地頁上,進入泰格 - 視頻 - 在text_field_tag
  2. 控制器會 - 虎視頻 - 與搜索
  3. 用戶的土地將結果顯示在另一頁...頁

  4. 用戶的土地,進入查詢

index.html.erb

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

-

  1. 控制器需要查詢和搜索

youtubecontroller.rb

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

-

我的路線文件:

ActionController::Routing::Routes.draw do |map| 
map.search 「search」, :controller => 「youtube」 

我沒有帶得步驟3尚未..

我要的是隻是一個文本框的網頁和一個提交按鈕,當用戶輸入文字並提交,它們被帶到一個呈現10個YouTube結果的新頁面。任何幫助深表感謝!!!

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

回答

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 } 
+1

嗨,謝謝你的迴應!我能夠得到結果顯示..但search.html.erb中的表單不顯示..當我導航到http://0.0.0.0:3000/youtube/search而不是表單顯示時,它會自動呈現搜索結果..這是它吐出來的 #,@media_content = [#,@ default = true,@ url =「http://www.youtube.com/v/NkfuPTm1zUo?f=videos&app=youtube_gdata」>] ,@ title =「X-Men Evolution:Season 3,Episode 3」,@ published_at = Wed Mar 04 20:45:26 UTC 2009,@ duration = 1285,@thumbnails = [#,#,#,#],@ player_url =「http://www.youtube.com/watch?v = NkfuPTm1zUo&feature = youtube_gdat ... – Felix 2010-10-09 01:52:01

+1

對不起,我沒有測試,我只是更新了我的代碼,請再看一遍,我測試過了,沒關係。希望它對你有所幫助。:) – khanh 2010-10-09 15:37:05