【问题标题】:Sorting tweets by re-tweets/favourites按转发推文/收藏夹对推文进行排序
【发布时间】: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 的编辑帮助

标签: ruby twitter sinatra erb


【解决方案1】:

我对 sinatra 不是特别熟悉,但看起来您将表单方法设置为 post,但 /list_of_tweets 路由没有 post 处理程序。这个答案对定义与动词无关的处理程序有一些建议:

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

另外,可能更正确,因为此操作似乎是只读的,您可以将表单上的方法设置为“get”而不是“post”。见http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

<form method="get">

【讨论】:

  • 将表单上的方法更改为“get”不起作用,不幸的是@alexcavalli
  • 嗯。您是否还在看到带有“Sinatra 不知道这个小曲子”的 sinatra 404 页面。在上面,还是别的什么?
  • 我有一个错误,显示语句“此页面不存在”。 @alexcavalli
  • @skg22 是“此页面不存在”。呈现给浏览器的所有内容(或者该文本是否嵌入在您的一个页面中)?你能告诉我你用的是什么版本的sinatra吗?我会尝试重现。
  • 我正在使用 RubyMine 和 sinatra gem 版本 1.4.5 @alexcavalli
【解决方案2】:

.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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2014-10-13
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多