Pagination in rails 3 - ruby-on-rails

In my rails application I need to display the matching tweets .lets say for example the matching results has 50 records, I need to display 10 records per page.I am getting the output which has all results but when I use pagination its showing link to different pages , but when I click the link to next page it says "string not matched". I tried different combinations like 5 per page ,but when I click the link to the next page it says "string not matched", but when I try without pagination it shows all the results
My code for the controller
class TweetsController<ApplicationController
def index
city = params[:show]
search_term = params[:text]
search_term[" "] = "%"
#tweets = Tweets.where("tweet_text LIKE? ", "%#{search_term}%").paginate( page: params[:page], per_page: 3)
My code for the view
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
Anyone please help me with this

You have an error in
search_term[" "] = "%"
line. If it should replace whitespace with "%", it should be:
search_term.gsub!(/\s/, '%')

Related

How to embed instance method with link_to text field?

On erb:
#student.count #--> 4
<li><%= link_to "Show All", 'all' %></li>#-->Show all
I want to show
show All(3)
.
I tried
<li><%= link_to "Show All"#students.count, 'all' %></li>
or
<li><%= link_to "Show All"+#students.count, 'all' %></li>
But all didn't work
Use interpolation
<li><%= link_to "Show All(#{#student.count})", 'all' %></li>
This doesn't work because you are concatenating a string and a number (TypeError: no implicit conversion of Fixnum into String)
"Show All" + #students.count
This works
"Show All" + #students.count.to_s
TRY
<li><%= link_to "Show All(" + #students.count.to_s + ")" , 'all' %></li>
OR
<li><%= link_to "Show All(#{#student.count})", 'all' %></li>

How do I DRY up my active nav links?

I am looking to pass this into the controller so I don't have to repeat or ask for path names.
<ul class="nav navbar-nav">
<li><%= link_to 'Breakfast', '/breakfast', class: ('active' if request.path == breakfast_path) %></li>
<li><%= link_to 'Lunch', '/lunch', class: ('active' if request.path == lunch_path) %></li>
<li><%= link_to 'Deli', '/deli', class: ('active' if request.path == deli_path) %></li>
<li><%= link_to 'Contact', '/contact', class: ('active' if request.path == contact_path) %></li>
</ul>
Gems will not be considered an answer...
Thanks!
I'm not sure passing it into the controller is a good solution - it's concerned with view logic so I think the view is the right level to handle it on. Perhaps creating a helper method that wraps link_to would be the right approach. Here's an idea without having tested it:
# YourHelper
def nav_link_to(label, path)
link_to(label, path, class: 'active' if path == request.path)
end
You could use the active_link_to gem:
<% food = %w(breakfast lunch deli contact) %>
<ul class="nav navbar-nav">
<% food.each do |meal| %>
<li><%= active_link_to meal.titleize, eval("#{meal}_path") %></li>
<% end %>
</ul>

Add param to link_to show method

I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>

Rails 3 taking a long time to load the results

My rails application has to query through a database which has lacks of rows and display the matching results. My code is working perfectly but its dead slow ,its taking a very long time to show the results.I am using ruby 1.9.3 and rails 3.2.13 and "Webrick" server.I am sure there is some problem . Is there any way I can resolve this .I have two tables called coordinates and tweets.
My code for the controller
require 'will_paginate/array'
class TweetsController<ApplicationController
def index
city = params[:show]
search_term = params[:text]
search_term.gsub!(/\s/, '%')
city_coordinates = Coordinates.where('city=?', city)
if (city_coordinates.count == 1 && city_coordinates.first.valid_location?)
#tweets = ((Tweets.for_coordinates(city_coordinates.first) & Tweets.where("tweet_text LIKE?" , "%#{search_term}%"))).paginate(page: params[:page], per_page:5)
elsif(city_coordinates.count!=1)
#tweets = ((Tweets.for_user_location(city) & Tweets.where("tweet_text LIKE?" , "%#{search_term}%"))).paginate(page: params[:page], per_page: 5)
else
Tweets.where("tweet_text LIKE? ", "%#{search_term}%").paginate(page: params[:page], per_page: 5)
end
end
end
My code for the model
class Tweets<ActiveRecord::Base
attr_accessible :id, :tweet_created_at, :tweet_id, :tweet_text, :tweet_source, :user_id, :user_name, :user_sc_name, :user_loc, :user_img, :longitude, :latitude, :place, :country
def self.for_coordinates(coordinates)
bbox = { min_lat: coordinates.latitude - 1.0, max_lat: coordinates.latitude + 1.0,
min_lng: coordinates.longitude - 1.0, max_lng: coordinates.longitude + 1.0
}
Tweets.where("(longitude BETWEEN ? and ?) AND (latitude BETWEEN ? and ?) OR (user_loc LIKE ?) " ,
bbox[:min_lng], bbox[:max_lng], bbox[:min_lat], bbox[:max_lat], "%#{coordinates.city}%" )
end
def self.for_user_location(city)
#tweets= Tweets.where("user_loc LIKE ?", "%#{city}%")
end
end
My code for the view
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
It has two search boxes ,one for the tweet query and another for the city,It has to display tweets for a particular city.But its very slow .I am not able to find the reason if it is due to inefficient coding or wrong choice of server or some other reasons .It uses Mysql database.
Your issue is with the union of two queries:
#tweets = (
(
Tweets.for_coordinates(city_coordinates.first) &
Tweets.where("tweet_text LIKE?" , "%#{search_term}%")
)
).paginate(page: params[:page], per_page:5)
What this does is:
Retrieve all tweets for the city coordinates into an in-memory array
Retrieve all tweets where the text matches the search term into an in-memory array
Concatenates the arrays from 1. and 2.
Paginates these results
What you want to do instead is:
Filter for tweets that match the city coordinates AND the search term
Paginate the results
Retrieve the paginated results
You'll want something more like this:
#tweets = Tweets.for_coordinates(city_coordinates.first).
where('tweet_text like ?', "%#{search_term}%").
paginate(page:params[:page], per_page:5)

ruby on rails routing error routing to the wrong page

coordinates GET /coordinates(.:format) coordinates#index
POST /coordinates(.:format) coordinates#create
new_coordinate GET /coordinates/new(.:format) coordinates#new
edit_coordinate GET /coordinates/:id/edit(.:format) coordinates#edit
coordinate GET /coordinates/:id(.:format) coordinates#show
PUT /coordinates/:id(.:format) coordinates#update
DELETE /coordinates/:id(.:format) coordinates#destroy
tweets_search GET /tweets/search(.:format) tweets#search
tweets_index GET /tweets/index(.:format) tweets#index
class TweetsController<ApplicationController
def index
#include 'coordinates_controller.rb'
include SearchHelper
include ParamasHelper
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
In my Rails app, I have two tables named Coordinates and Tweets. I have four actions to be rendered.
My routes.rb file
Tweetsblog2::Application.routes.draw do
resources :tweets, :coordinates
get "tweets/show"
get "tweets/index"
match "/tweets/show" => "tweets#show"
match "/tweets/index" => "tweets#index"
Whenever I navigate to http://localhost:3000/tweets, it's showing tweets/index instead of tweets/show and the same error I am getting with different names.
When I navigate to http://localhost:3000/tweets/show, it's giving ArgumentError in TweetsController#show.
When I navigate to http://localhost:3000/tweets/index its giving ArgumentError in TweetsController#show same thing
My code for show.html.erb:
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= text_field_tag(:search) %>
<%= submit_tag("get results ") %>
<% end %>
My code for index.html.erb:
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
It's not routing to the proper page. Please help me, I am stuck with this.
you have to write
resources :tweets, except: [:index,:show]
because you declared you resource first, so rails is trying to match its default routing instead of your custom action:
get "tweets/index"
-updating per comment-
Tweetsblog2::Application.routes.draw do
resources :coordinates
get "tweets/show" => "tweets#show"
get "tweets/index" => "tweets#index"
Removing the resources :tweets should fix your issue and allow you to use just call the 2 options. The initial resources :tweets tells rails that you wanted resourceful routes (index shows all of the resource, show a specify one, etc..). So just building out the 2 non-resourceful routes as above sounds like what you want. The way I normally do something like this is to just include the search form in in the index page and if there are no search params then show all tweets.)

Resources