尝试创建新的推文以显示在我的索引中,但我不想离开页面。我的应用程序所需的功能是跟踪来自推文流的推文,并自动将该信息传递到我的推文对象并保存到数据库。
控制器:
class TweetsController < ApplicationController
TWITTER_COMSUMER_KEY = "GfqdzJKb5kIyEnYlQuNGlg"
TWITTER_CONSUMER_SECRET = "A3Fe0IvDbhlKgowCVmV1WVLlcdYgQ8w9clrDSegCQ"
TWITTER_OATH_TOKEN = "34012133-caUYq3eiNC7Z9L9KvTgG51VgyctqVxkXP0tKIXDk0"
TWITTER_OATH_TOKEN_SECRET = "DSLA3F8BPssyEeEP2wZgQ1OJRL5kIVPZfON4GYZFw"
TweetStream.configure do |config|
config.consumer_key = TWITTER_COMSUMER_KEY
config.consumer_secret = TWITTER_CONSUMER_SECRET
config.oauth_token = TWITTER_OATH_TOKEN
config.oauth_token_secret = TWITTER_OATH_TOKEN_SECRET
end
def index
@tweets = Tweet.all
end
def new
@tweet = Tweet.new
end
def create
TweetStream.track('bed', 'morning', 'breakfast') do |status|
temp = status.text
if(temp.include? "http")
@tweet = Tweet.new(status.text)
if @tweet.save
else
render "new"
end
end
end
end
def show
end
end
索引.html.erb
<h1>Tweet Tracker</h1>
<% @tweets.each do |tweet| %>
<p><%= tweet.content %></p>
<hr />
<% end %>