0

I'm trying to implement a method whereby a user can manipulate three radio buttons, each with assigned methods, to sort the tweets either by retweets, by favourites or default(by date).

Currently, the page lists the tweets by date as default but every time I pressed either of the two buttons, I get the error "This page does not exist."

My .rb file is:

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

My tweets_list.erb file is:

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
skg22
  • 37
  • 8
  • Welcome to Stack Overflow. When creating a title for a question, it isn't necessary to artificially add the tags to the sentence. Instead, create a grammatically correct sentence. The tags get added automatically by "the system" for you. – the Tin Man Feb 25 '15 at 21:49
  • Noted. Thanks for the editing aid @theTinMan – skg22 Feb 25 '15 at 22:04

2 Answers2

0

I'm not especially familiar with sinatra, but it looks like you're setting the form method to post but there is no post handler for the /list_of_tweets route. This answer has some suggestions for defining verb-agnostic handlers:

https://stackoverflow.com/a/8424097/4280232

You could try this

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

Alternatively, and probably more correctly as this operation appears to be read-only, you could set the method on the form to "get" instead of "post". See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

<form method="get">
Community
  • 1
  • 1
alexcavalli
  • 526
  • 6
  • 10
  • Changing the method on the form to "get" doesn't work, unfortunately @alexcavalli – skg22 Feb 25 '15 at 22:48
  • Hmm. Are you still seeing the sinatra 404 page with "Sinatra doesn’t know this ditty." on it, or something else? – alexcavalli Feb 25 '15 at 23:08
  • I have an error which displays the statement, "This page does not exist." @alexcavalli – skg22 Feb 25 '15 at 23:56
  • @skg22 Is "This page does not exist." everything that's getting rendered to the browser (or is that text embedded in one of your pages)? Can you tell me what version of sinatra you're using? I'll try to reproduce. – alexcavalli Feb 26 '15 at 00:05
  • I'm using RubyMine with sinatra gem version 1.4.5 @alexcavalli – skg22 Feb 26 '15 at 09:24
  • I'm using that exact same version. I made these two test files, which is your setup minus the twitter part, and it works for me: https://gist.github.com/alexcavalli/31ada9f3f7e8cd2953d2 (but if I change the form method to post I get a 404 page) Can you provide any more information? Are there other pages in your application? If you comment out the twitter parts does it work? – alexcavalli Feb 26 '15 at 16:01
  • My original files have href attributes which allows user to go to any of the other 5 pages from the current page. The 2 codes I posted are test files because out of the 6 pages, this is the only one that's giving me problems @alexcavalli – skg22 Feb 26 '15 at 16:10
  • Does this problem occur if you click the form submit button with the "default" option selected? I.e. does it occur whenever you click the button, regardless of the option? – alexcavalli Feb 26 '15 at 16:43
0

.rb file:

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 file:

<!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 %>
skg22
  • 37
  • 8