2010-10-07 88 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