I am using Rails 3.0.7, kaminari gem.
I just built a simple site, with SQLite database and 100,000 rows of data, with just four columns (ID, name, created_at, updated_at).
This is my controller:
class HclinksController < ApplicationController
# GET /hclinks
# GET /hclinks.xml
def index
#hclinks = Hclink.page(params[:page])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #hclinks }
end
end
end
I scaffolded it.
Here's my log:
// THIS IS INDEX, WHICH SHOWS ALL THE LIST ITEMS
Started GET "/hclinks" for 127.0.0.1 at 2011-05-08 00:59:52 +0800
Processing by HclinksController#index as HTML
Hclink Load (391.8ms) SELECT "hclinks".* FROM "hclinks" LIMIT 25 OFFSET 0
SQL (4574.8ms) SELECT COUNT(*) FROM "hclinks"
Rendered hclinks/index.html.erb within layouts/application (178778.1ms)
Completed 200 OK in 182775ms (Views: 175845.5ms | ActiveRecord: 4966.6ms)
// THIS IS THE RUBY ON RAILS WELCOME PAGE
Started GET "/hclinks" for 127.0.0.1 at 2011-05-08 01:03:13 +0800
Processing by HclinksController#index as HTML
Hclink Load (65.3ms) SELECT "hclinks".* FROM "hclinks" LIMIT 25 OFFSET 0
SQL (910.6ms) SELECT COUNT(*) FROM "hclinks"
Rendered hclinks/index.html.erb within layouts/application (1532.2ms)
Completed 200 OK in 1657ms (Views: 652.5ms | ActiveRecord: 975.9ms)
I wonder why the list took so long to respond... How should I fix it? Thanks!
Is this log output from a fresh restart of rails or had the app already run a few requests?
If this wasn't the first request, I'd check if the load time increases on each request.
If it does, you may have a memory leak
Related
I have a simple photo gallery on a site, and am having some trouble when I invoke either the edit or delete paths. I click on the button and the page acts like it is going to load, then goes to an error page. Here is the trace from the console:
Started GET "/photos/17/edit" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Started GET "/photos/17/edit" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Processing by PhotosController#edit as HTML
Processing by PhotosController#edit as HTML
Parameters: {"id"=>"17"}
Parameters: {"id"=>"17"}
Photo Load (0.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = $1 LIMIT 1 [["id", 17]]
Photo Load (0.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = $1 LIMIT 1 [["id", 17]]
Album Load (7.9ms) SELECT "albums".* FROM "albums"
Album Load (7.9ms) SELECT "albums".* FROM "albums"
Rendered photos/_form.haml (35.7ms)
Rendered photos/_form.haml (35.7ms)
Rendered photos/edit.haml within layouts/application (38.9ms)
Rendered photos/edit.haml within layouts/application (38.9ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered layouts/_navbar.html.haml (5.0ms)
Rendered layouts/_navbar.html.haml (5.0ms)
Completed 200 OK in 187ms (Views: 174.4ms | ActiveRecord: 9.4ms)
Completed 200 OK in 187ms (Views: 174.4ms | ActiveRecord: 9.4ms)
Then I get this immediately after, before the edit page loads:
Started GET "/undefined" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Started GET "/undefined" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
ActionController::RoutingError (No route matches [GET] "/undefined"):
I've looked over my controller and methods and haven't seen anything that would suggest the need for it to look for the /undefined path. What is worse is that it doesn't happen every time, which made me think there was something wrong with my ruby installation or something. If that was the case, it would not give the error on Heroku (which it does) and would probably error out in other applications I work on (which it doesn't).
Controller:
def update
respond_to do |format|
if #photo.update(photo_params)
format.html { redirect_to #photo, notice: 'Photo was successfully updated.' }
format.json { render :show, status: :ok, location: #photo }
else
format.html { render :edit }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
def destroy
#photo.destroy
respond_to do |format|
format.html { redirect_to photos_path, notice: 'Photo was successfully destroyed.' }
format.json { head :no_content }
end
end
Model:
class Photo < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :album
validates_presence_of :image
end
When I notice that it is redirecting me, I can just browse to the picture itself fine (photos/8), but the edit path is still kicking me to the /undefined (photos/8/edit).
I realise (and hope) you might have solved this already, but I've been banging my head against the wall due to a similar problem - a GET request to /undefined route. Luckily, I was able to solve it and this might help people with similar problems.
To explain my situation - I have some AJAX calls and I suspected it might have something to do with that, even though I did not build up any URLs with variables that could be undefined.. or so I thought (I was partially right, though).
I was fetching some information (including image URLs to be rendered in view thereafter) based on data returned by an API in JSON format. I hope you see where I am going with this :)
Turns out one image (for that specific API request, I am sure there are other such cases which I haven't yet found) had no URL defined. As I passed the json data to a constructor, I expected the image URL to be defined and inserted that into the image src attribute. The URL was, however, apparently undefined. Therefore, instead of loading the image via absolute path to the API's CDN, as the src had no http(s)://, it tried to load it from local server. As the variable with image url was undefined, the image src attribute pointed to a relative /undefined route. Hence a GET request to "/undefined" route.
I should note that this was not an ajax call per se, the get request was a result of using data from ajax call to construct an image src attribute. The fact that I used that data for constructing a google maps infowindow HTML structure (which included an image in it) only compounded the problem as the missing image (due to undefined src) would only show up after clicking on a specific google maps marker.
So, for your case (while it might not be relevant for you anymore, might be for someone else) - make sure you look at image src attributes as images will start loading immediately after your page (or in case of ajax loading and constructing HTML image tags afterwards) loads. Hence one 'normal' request that gets processed as it should. If, however, an image src attribute is "undefined", it will then immediately start a get request to "/undefined" route.
I get this in my server log, which seems to indicate that it returned ajaxresponse.js.erb:
Started GET "/ajaxresponse" for 70.28.21.25 at 2013-10-01 15:38:54 +0000
Processing by ContentController#ajaxresponse as JS
Rendered content/ajaxresponse.js.erb within layouts/content (0.5ms)
Rendered inc/_analytics.erb (0.3ms)
Completed 200 OK in 46ms (Views: 45.3ms | ActiveRecord: 0.0ms)
Inside ajaxresponse.js.erb :
$('#ajaxdiv').append("fdsa");
There is a div on the page with ID "ajaxdiv"
but it doesn't seem to change anything? I've tried 100 different javascript commands within the .js.erb file, but none of them are affecting the page.
Controller action:
def ajaxresponse
respond_to do |format|
format.js {}
end
end
Chrome console log :
Uncaught Error: jquery-ujs has already been loaded!
Edit: Just remoted jquery-ujs from the application.rb file and there are no more errors. The page, however, still doesn't update with ajax
The ajaxdiv is below the save button and has default text: "ohai"
To get a div with the id "ajaxdiv", you need to use #ajaxdiv, not ajaxdiv:
$('#ajaxdiv').append("fdsa");
Okay, here's the deal I can't figure out at the moment.
So, I have this action looking like this:
def get
#page = Page.find_by_title(params[:title])
respond_to do |format|
format.html # get.html.erb
format.js # get.js.coffee
end
end
Which, depending on whether it's an AJAX call or a normal GET request, the renders either get.html.erb or get.js.coffee.
In development, that is, as shown by the following log entry:
Started GET "/pages/medien/get" for 127.0.0.1 at 2011-12-11 18:58:31 +0100
Processing by PagesController#get as JS
Parameters: {"title"=>"medien"}
Rendered pages/_get.html.erb (153.0ms)
Rendered pages/get.js.coffee (1185.0ms)
Completed 200 OK in 1230ms (Views: 1220.0ms | ActiveRecord: 5.0ms)
In production the same request and same code results in a log entry like this:
Started GET "/pages/medien/get/" for 91.11.86.230 at 2011-12-11 18:57:44 +0100
Processing by PagesController#get as JS
Parameters: {"title"=>"medien"}
Read fragment views/mypage/pages/medien/get (0.1ms)
Rendered pages/_get.html.erb (0.8ms)
Rendered pages/get.html.erb (0.9ms)
Completed 200 OK in 2ms (Views: 1.5ms | ActiveRecord: 0.2ms)
I simply don't get why it's even stating that it's processing it as JavaScript but then does not execute the javascript in get.js.coffee without even throwing an error!
I've tried looking at other answers for this but I can't seem to figure out why my redirect isn't working.
So I'm using Devise with Rails 3.1, and I'm making a shopping site. Visitors aren't allowed to add things to their cart unless they are signed in. This is what I'm having trouble with: if they aren't signed in, I want to redirect them to the Items index page. Here's what I have:
class ItemsController < ApplicationController
def add_to_cart
#item = Item.find(params[:id])
if current_user
#item.update_attributes(:cart_id => #current_cart.id)
redirect_to :back
else
redirect_to categories_path, notice: 'You must sign in to add an item to your cart.'
end
end
.
.
.
end
As of right now, when I click the link to add to cart, the method gets executed (I can see Rails loading and defining #item in the server log), and it reaches the 'else' statement, but no redirect happens.
I've already generated scaffolding for the index, new, etc. (all the RESTful actions). Also, I'm sure that I'm reaching the add_to_cart method because I've tried debugging with some puts statements. What's happening here?
EDIT:
Also, another weird thing which may be of use... The server seems to try to execute this method twice, and tries to 'get' categories twice:
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 26ms
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.2ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 25ms
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 35ms (Views: 28.5ms | ActiveRecord: 4.2ms)
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 37ms (Views: 30.6ms | ActiveRecord: 4.2ms)
EDIT 2 (as requested by Delba)
resources :items do
member do
get 'add_to_cart'
end
end
EDIT 3: changing the else statement to respond to javascript
respond_to do |format|
format.js { redirect_to items_path, notice: 'You must sign in to add an item to your cart.' }
end
For anyone who may need answers to this question, simply replace redirect_to statements with the following:
respond_to do |format|
format.js
end
Then, in your views under items, make a add_to_cart.js.erb page, consisting of javascript to make notices, or do whatever. Here's what I put in mine:
alert("Need to be signed in")
EDIT: Also, for the part where it executes twice: this is somewhat unrelated, but for some reason by default Rails includes duplicate Javascripts. Specifically, look at application.js: it says require jquery and require jquery_ujs. Disable one of these and you're home free.
To disable one of these javascripts:
Go to assets/application.js
Remove the comments (the // ) before require jquery, require tree .
This way, Rails doesn't assume the default and instead includes only jquery and whatever other javascripts you have in assets/javascripts
in my test mongrel server output for a page, 8 queries are listed but many more DB are counted:
Query1
Query2
...
Query8
Rendered Partial1
Rendered Partial2
..
Rendered Partial40
Completed in 4754ms (View: 308, DB: 2246) | 200 OK
how do I show all the queries that are running?
Also, is there documentation for what the View; count represents?
I'm not sure I understand the question, but are you asking what the number after DB means? If so, it's the number of records returned.
Some sample output from my Mongrel server:
Processing AlbumsController#show to xml (for 127.0.0.1 at 2009-12-22 06:44:38) [GET]
Parameters: {"format"=>"xml", "action"=>"show", "id"=>"1", "controller"=>"albums"}
Album Load (0.1ms) SELECT * FROM `albums` WHERE (`albums`.`id` = '1') LIMIT 1
Album Columns (0.8ms) SHOW FIELDS FROM `albums`
Artist Columns (2.0ms) SHOW FIELDS FROM `artists`
Artist Load (0.1ms) SELECT * FROM `artists` WHERE (`artists`.`id` = 1)
Track Load (0.1ms) SELECT * FROM `tracks` WHERE (`tracks`.album_id = 1)
Track Columns (1.3ms) SHOW FIELDS FROM `tracks`
Completed in 48ms (View: 1, DB: 22) | 200 OK [http://localhost/albums/1.xml]
You see only 3 queries here, but 22 records are returned. If you want to see all of the records that are being shown, display them in your templates. Or, you could easily display them in XML in your controller.
def index
#records = Record.find(:all)
respond_to do |format|
format.xml { render :xml => #records }
end
end
Long story short, you are seeing all of the queries.