我正在尝试实现一种方法,用户可以操作三个单选按钮,每个按钮都有指定的方法,通过转发,收藏或默认(按日期)对推文进行排序。
目前,该页面按日期列出了推文的默认值,但每当我按下两个按钮中的任何一个时,我都会收到错误“此页面不存在。”
我的.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 %>
答案 0 :(得分:0)
我对sinatra并不是特别熟悉,但您似乎已将表单方法设置为post
,但post
没有/list_of_tweets
处理程序路线。这个答案提供了一些定义动词不可知处理程序的建议:
https://stackoverflow.com/a/8424097/4280232
你可以试试这个
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
或者,可能更正确,因为此操作似乎是只读的,您可以将表单上的方法设置为&#34; get&#34;而不是&#34; post&#34;。见http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
<form method="get">
答案 1 :(得分:0)
.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 %>