我是Ruby on Rails的新手。我有一個簡單的應用程序連接到Twitter。該應用程序在主頁上有一個搜索文本框和一個提交按鈕。同樣還有一個模型和控制器。我想在twitter上動態搜索一些關鍵字,然後將它們顯示在我們應用程序的主頁上。我在「index.html.erb」頁面中創建了一個文本框並提交按鈕。我不知道如何取得關鍵字並傳入控制器。然後我想顯示搜索結果。任何人都可以爲同樣的網站舉例嗎?請建議代碼中的錯誤/更改。像 - 這Ruby on Rails的Ruby On Rails - 從文本框中檢索數據顯示其內容
的index.html.erb
<h1>Tweets about New Year Resolution</h1>
<tr><td>Enter keyword <input type = "text" name = "keyword"></td></tr>
<tr><td><input type = "submit" value = "SEARCH"></td></tr>
<%= form_tag(tweets/index, :method => "get") do %>
<%= label_tag(:q, "Enter keyword :") %>
<%= text_field_tag(:q) %>
<%= submit_tag("SEARCH") %>
<% end %>
<div id="container">
<ul>
<% @tweets.each do |tweet| %>
<li class="<%=cycle('odd', '')%>">
<%= link_to tweet.from_user, "http://twitter.com/#{tweet.from_user}", :class => "username", :target => "_blank" %>
<div class="tweet_text_area">
<div class="tweet_text">
<%=raw display_content_with_links(tweet.text) %>
</div>
<div class="tweet_created_at">
<%= time_ago_in_words tweet.twitter_created_at %> ago
</div>
</div>
</li>
<% end %>
</ul>
</div>
控制器tweets.rb
class TweetsController < ApplicationController
def index
#Get the tweets (records) from the model Ordered by 'twitter_created_at' descending
searchValue = params[:keyword]
if Tweet.count > 0
Tweet.delete_all
end
Tweet.get_latest_new_year_resolution_tweets("iphone")
@tweets = Tweet.order("twitter_created_at desc")
end
end
我想你應該改變'searchValue =參數[:關鍵字]''到searchValue =參數[:Q]'搶輸入,並刪除'TD/tr'部分 – Bohdan 2012-07-31 06:43:06