我正在尝试构建一个在 Twitter 上搜索图像的应用程序。在我到达那里之前,我需要跳过构建一个将值输入函数的工作搜索字段的令人生畏的障碍。
在index.html.erb
由 控制的文件中Pages controller
,我有以下代码:
<%= form_tag root_path ({:controller => "pages", :action => "search", :method => "get", :class => "grabTweets" }) do %>
<%= text_field_tag :tweets, params[:tweets] %>
<% end %>
在我的Pages controller
我有:
def search
def grabTweets
@tweet = Twitter.search(params[:tweets] + "[pic] "+" path.com/p/", :rpp => 3, :result_type => "recent").map do |status|
@tweet = "#{status.text}" #class = string
@urls = URI::extract(@tweet, "http") #returns an array of strings
end
end
end
两个问题:
执行搜索后,我的搜索查询没有被传递到 grabTweets 函数中,正如 URL 所证明的那样:
http://localhost:3000/?action=search&class=grabTweets&method=get
(搜索查询是单词“bridge”
我的代码有什么问题,搜索查询没有正确执行。
考虑到我希望搜索结果显示在 中并且不打算更改页面,我应该将控制器中的代码放在def home
or中吗?def search
root_path
在此先感谢您的帮助!我完全被难住了。