【问题标题】:how to use youtube-g ruby gem?如何使用 youtube-g ruby​​ gem?
【发布时间】:2010-10-07 05:00:48
【问题描述】:

刚接触 Rails 并试图让 youtube-g 工作......我很难将用户搜索从 form_tag 传递到控制器......例如:

  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/

【问题讨论】:

    标签: ruby-on-rails ruby api youtube


    【解决方案1】:

    search.html.erb

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

    Youtube控制器

    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 }
    

    【讨论】:

    • 您好,感谢您的回复!我能够显示结果.. 但是 search.html.erb 中的表单不显示.. 当我导航到 0.0.0.0:3000/youtube/search 而不是显示表单时,它会自动呈现搜索结果.. 这就是它吐出的内容# , @media_content=[#, @default=true, @url="youtube.com/v/…, @title="X-Men Evolution: Season 3, Episode 3", @published_at=2009 年 3 月 4 日星期三 20:45:26 UTC, @duration=1285, @thumbnails=[#, #, #, #], @player_url="youtube.com/watch?v=NkfuPTm1zUo&feature=youtube_gdat...
    • 对不起。我不测试。我刚刚更新了我的代码。请再看一遍。我测试了它。没关系。希望对您有所帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多