按 re-tweets/favourites 对推文进行排序
Sorting tweets by re-tweets/favourites
我正在尝试实现一种方法,用户可以通过该方法操作三个单选按钮,每个单选按钮都有指定的方法,以按转发、按收藏夹或默认(按日期)对推文进行排序。
目前,该页面默认按日期列出推文,但每次我按下这两个按钮中的任何一个时,我都会收到错误 "This page does not exist."
我的 .rb 文件是:
require 'sinatra'
require 'twitter'
require 'erb'
include ERB::Util
config = {
:consumer_key => '..' ,
:consumer_secret => '..' ,
:access_token => '..' ,
:access_token_secret => '..'
}
client = Twitter::REST::Client.new(config)
get '/list_of_tweets' do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
erb :tweets_list
end
我的 tweets_list.erb 文件是:
<!DOCTYPE html>
<html>
<head>
<title>Twitter Interface</title>
</head>
<body>
<h1>List of Tweets</h1>
<form method="post">
<h3>Sort Tweets by</h3>
<input type="radio" name="operation" value="favorite_count" checked/>Favourites
<input type="radio" name="operation" value="retweet_count"/>Retweets
<input type="radio" name="operation" value="default"/>Default
<input type="submit" value="submit"/>
</form>
<% unless @history.nil? %>
<% if @params[:operation] == "favourite_count"%>
<% @history = tweets.take(20).sort_by!{|tweet| tweet.favorite_count} %>
<% else %>
<% @history.reverse! %>
<% end %>
<% if @params[:operation] == "retweet_count" %>
<% @history = tweets.take(20).sort_by!{|tweet| tweet.favorite_count} %>
<% else %>
<% @history.reverse! %>
<% end %>
<% if @params[:operation] == "default" %>
<% @history = tweets.take(20) %>
<% else %>
<% @history.reverse! %>
<% end %>
<table border="1">
<tr>
<th>Time Posted</th>
<th>Description of Tweets</th>
<th>Number of Retweets</th>
<th>Number of Favourites</th>
</tr>
<% @history.each do |tweet| %>
<tr>
<td><%= tweet.created_at %></td>
<td><%= tweet.text %></td>
<td><%= tweet.retweet_count %></td>
<td><%= tweet.favorite_count %> </td>
</tr>
<% end %>
</table>
<% end %>
我对 sinatra 不是特别熟悉,但看起来您将表单方法设置为 post
,但 /list_of_tweets
路由没有 post
处理程序。这个答案对定义与动词无关的处理程序有一些建议:
你可以试试这个
list_of_tweets = lambda do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
erb :tweets_list
end
get '/list_of_tweets', &list_of_tweets
post '/list_of_tweets', &list_of_tweets
或者,并且可能更正确,因为此操作似乎是只读的,您可以将表单上的方法设置为 "get" 而不是 "post"。参见 http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
<form method="get">
.rb 文件:
require 'sinatra'
require 'twitter'
require 'erb'
include ERB::Util
config = {
:consumer_key => '..' ,
:consumer_secret => '..' ,
:access_token => '..' ,
:access_token_secret => '..'
}
client = Twitter::REST::Client.new(config)
get '/list_of_tweets' do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
unless @params[:operation].nil?
puts "selected #{@params[:operation]}"
if @params[:operation] == "favorite_count"
@history.sort_by!{|tweet| tweet.favorite_count}
@history.reverse!
elsif @params[:operation] == "retweet_count"
@history.sort_by!{|tweet| tweet.retweet_count}
@history.reverse!
elsif @params[:operation] == "default"
puts "default"
end
end
erb :tweets_list
end
.erb 档案:
<!DOCTYPE html>
<html>
<head>
<title>Twitter Interface</title>
</head>
<body>
<h1>List of Tweets</h1>
<form method="post">
<h3>Sort Tweets by</h3>
<input type="radio" name="operation" value="favorite_count" checked/>Favourites
<input type="radio" name="operation" value="retweet_count"/>Retweets
<input type="radio" name="operation" value="default"/>Default
<input type="submit" value="submit"/>
</form>
<table border="1">
<tr>
<th>Time Posted</th>
<th>Description of Tweets</th>
<th>Number of Retweets</th>
<th>Number of Favourites</th>
</tr>
<% @history.each do |tweet| %>
<tr>
<td><%= tweet.created_at %></td>
<td><%= tweet.text %></td>
<td><%= tweet.retweet_count %></td>
<td><%= tweet.favorite_count %> </td>
</tr>
<% end %>
</table>
<% end %>
我正在尝试实现一种方法,用户可以通过该方法操作三个单选按钮,每个单选按钮都有指定的方法,以按转发、按收藏夹或默认(按日期)对推文进行排序。
目前,该页面默认按日期列出推文,但每次我按下这两个按钮中的任何一个时,我都会收到错误 "This page does not exist."
我的 .rb 文件是:
require 'sinatra'
require 'twitter'
require 'erb'
include ERB::Util
config = {
:consumer_key => '..' ,
:consumer_secret => '..' ,
:access_token => '..' ,
:access_token_secret => '..'
}
client = Twitter::REST::Client.new(config)
get '/list_of_tweets' do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
erb :tweets_list
end
我的 tweets_list.erb 文件是:
<!DOCTYPE html>
<html>
<head>
<title>Twitter Interface</title>
</head>
<body>
<h1>List of Tweets</h1>
<form method="post">
<h3>Sort Tweets by</h3>
<input type="radio" name="operation" value="favorite_count" checked/>Favourites
<input type="radio" name="operation" value="retweet_count"/>Retweets
<input type="radio" name="operation" value="default"/>Default
<input type="submit" value="submit"/>
</form>
<% unless @history.nil? %>
<% if @params[:operation] == "favourite_count"%>
<% @history = tweets.take(20).sort_by!{|tweet| tweet.favorite_count} %>
<% else %>
<% @history.reverse! %>
<% end %>
<% if @params[:operation] == "retweet_count" %>
<% @history = tweets.take(20).sort_by!{|tweet| tweet.favorite_count} %>
<% else %>
<% @history.reverse! %>
<% end %>
<% if @params[:operation] == "default" %>
<% @history = tweets.take(20) %>
<% else %>
<% @history.reverse! %>
<% end %>
<table border="1">
<tr>
<th>Time Posted</th>
<th>Description of Tweets</th>
<th>Number of Retweets</th>
<th>Number of Favourites</th>
</tr>
<% @history.each do |tweet| %>
<tr>
<td><%= tweet.created_at %></td>
<td><%= tweet.text %></td>
<td><%= tweet.retweet_count %></td>
<td><%= tweet.favorite_count %> </td>
</tr>
<% end %>
</table>
<% end %>
我对 sinatra 不是特别熟悉,但看起来您将表单方法设置为 post
,但 /list_of_tweets
路由没有 post
处理程序。这个答案对定义与动词无关的处理程序有一些建议:
你可以试试这个
list_of_tweets = lambda do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
erb :tweets_list
end
get '/list_of_tweets', &list_of_tweets
post '/list_of_tweets', &list_of_tweets
或者,并且可能更正确,因为此操作似乎是只读的,您可以将表单上的方法设置为 "get" 而不是 "post"。参见 http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
<form method="get">
.rb 文件:
require 'sinatra'
require 'twitter'
require 'erb'
include ERB::Util
config = {
:consumer_key => '..' ,
:consumer_secret => '..' ,
:access_token => '..' ,
:access_token_secret => '..'
}
client = Twitter::REST::Client.new(config)
get '/list_of_tweets' do
puts "Visiting history page..."
tweets = client.user_timeline(user)
@history = tweets.take(20)
unless @params[:operation].nil?
puts "selected #{@params[:operation]}"
if @params[:operation] == "favorite_count"
@history.sort_by!{|tweet| tweet.favorite_count}
@history.reverse!
elsif @params[:operation] == "retweet_count"
@history.sort_by!{|tweet| tweet.retweet_count}
@history.reverse!
elsif @params[:operation] == "default"
puts "default"
end
end
erb :tweets_list
end
.erb 档案:
<!DOCTYPE html>
<html>
<head>
<title>Twitter Interface</title>
</head>
<body>
<h1>List of Tweets</h1>
<form method="post">
<h3>Sort Tweets by</h3>
<input type="radio" name="operation" value="favorite_count" checked/>Favourites
<input type="radio" name="operation" value="retweet_count"/>Retweets
<input type="radio" name="operation" value="default"/>Default
<input type="submit" value="submit"/>
</form>
<table border="1">
<tr>
<th>Time Posted</th>
<th>Description of Tweets</th>
<th>Number of Retweets</th>
<th>Number of Favourites</th>
</tr>
<% @history.each do |tweet| %>
<tr>
<td><%= tweet.created_at %></td>
<td><%= tweet.text %></td>
<td><%= tweet.retweet_count %></td>
<td><%= tweet.favorite_count %> </td>
</tr>
<% end %>
</table>
<% end %>