【发布时间】:2015-04-28 02:26:09
【问题描述】:
我正在尝试实现一种方法,用户可以通过该方法操作三个单选按钮,每个按钮都有指定的方法,以通过转发、收藏或默认(按日期)对推文进行排序。
目前,该页面默认按日期列出推文,但每次我按下两个按钮中的任何一个时,都会收到错误消息“此页面不存在。”
我的 .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 %>
【问题讨论】:
-
欢迎来到 Stack Overflow。为问题创建标题时,无需人为地将标签添加到句子中。相反,创建一个语法正确的句子。标签由“系统”自动为您添加。
-
注明。感谢@theTinMan 的编辑帮助