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.
Related
IMPORTANT NOTE: The id does not matter, it is same in all cases.
I am only seeing the name field, not description, hours, etc even though these aren't null.
I have declared all standard routes through resources (with default format json), not individually.
I have even tried creating a projects/show.json.jbuilder file:
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername
My projects/show method:
#project = Project.find(params[:id])
render :json => #project
FIRST EDIT:
I added logger.debug right before defining #project in my show method.
Now in my command prompt window for the local server, I am seeing:
Started GET "/projects/12" for 127.0.0.1 at 2015-02-25 16:36:45 -0500
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"12"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 12]]
#<Project:0x007f95477e72f8>
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 51]]
Completed 200 OK in 42ms (Views: 15.2ms | ActiveRecord: 0.9ms)
I am wondering why I am seeing the "User Load" part, since my Project model does not belong to a User object or have any relationship with it (though in the past it may have had before that relationship was removed). Also, I don't think I saw
#<Project:0x007f95477e72f8>
earlier.
I solved my problem this way.
Basically, projects/12.json currently works and not projects/12. Since I am more interested in obtaining the data client side, it is ok for me. I would need a projects/show.html.erb page that calls all the project data through view helper methods, for the HTML to work.
def show
#project = Project.find(params[:id])
respond_to do |format|
format.html
format.json
end
end
projects/12.json works because I have a projects/show.json.jbuilder file (as I shared in my original question post):
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername
I have an HTML table that displays order items. On each table row I have a "Need By Date" field that is standalone(not in a form) input field. Whenever a user changes the date, I post this date to the server to save it against the order.
I am noticing that when I use OrderItem.update or OrderItem.update_attributes from the rails console it works, but when it is triggered from a field change event Rails does not trigger an update to the table.
#JavaScript
$(".order-item.datepicker").change ->
needby_date = $(this).val()
order_item_url = $(this).data("order-item-url")
$.ajax
type: "POST"
url: order_item_url
data:
needby_date: needby_date
My controller -
def update_needby_date
#order_item = OrderItem.find(params[:order_item_id])
#order_item.update_attribute(update_needby_date_params)
respond_to do |format|
format.js { render :nothing => true }
end
end
private
def update_needby_date_params
params.permit(:order_item_id, :needby_date)
end
As you can see in the log file, the update is not being triggered -
Started POST "/order_items/1/update_needby_date" for 127.0.0.1 at 2014-07-08 10:55:10 -0400
Processing by OrderItemsController#update_needby_date as */*
Parameters: {"needby_date"=>"07/23/2014", "order_item_id"=>"1"}
OrderItem Load (0.4ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."id" = ? LIMIT 1 [["id", "1"]]
(0.4ms) begin transaction
(0.2ms) commit transaction
Rendered text template (0.1ms)
Completed 200 OK in 16ms (Views: 3.3ms | ActiveRecord: 1.0ms)
There is #avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000'] variable (it is not instance of model), which represent avaliable options - colors, which hadn't been taken yet, through method:
def choose_color
#check=Check.find(params[:check_id])
#colorschemes=#check.colorschemes
current_colors=[]
#check.colorschemes.each do |c|
current_colors.push(c.color)
end
#avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000']
current_colors.each do |c|
if #avalibable_colors.index(c)
#avalibable_colors[#avalibable_colors.index(c)]=nil
end
end
#avalibable_colors.compact!
respond_to do |format|
format.js
end
end
After that #avalibable_colors contents only untaken color options.
And generally it works. If I choose 1 color option, it displays rest 4, if 2 - rest 3, if I choose one, and then delete it - it is in avaliable colors.
And that works until whole four. And If I add all four, and then delete one by one, no colors are avaliable until I refresh page.
Server logs:
Started GET "/choose_color?check_id=46" for 127.0.0.1 at 2013-06-13 19:00:31 +0400
Processing by ColorschemesController#choose_color as JS
Parameters: {"check_id"=>"46"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'oPXJtztDkYdYmVsQ3wxxfQ' LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
CACHE (0.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = $1 LIMIT 1 [["id", "46"]]
Colorscheme Load (0.0ms) SELECT "colorschemes".* FROM "colorschemes" WHERE "colorschemes"."check_id" = 46
Rendered colorschemes/_choose_color.html.erb (0.0ms)
Rendered colorschemes/choose_color.js.erb (5.0ms)
Completed 200 OK in 33ms (Views: 22.0ms | ActiveRecord: 3.0ms)
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
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