对rails很新,并尝试让youtube-g工作......我很难将用户搜索从form_tag传递给控制器......例如:
用户登陆显示结果的另一页...
用户登陆页面,输入查询
index.html.erb
<%= form_tag(youtube_path) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:wth) %>
<%= submit_tag("Search") %>
<% end %>
-
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结果。任何帮助非常感谢!!!
答案 0 :(得分: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 }