I want to debug my application and check what values are submitted on the form object in my controller class via logger ? How can it be done? Is there any better practice to debug rails application?
Your rails log (eg log/development.log) should already show everything that the form submitted, in the "Parameters". It will look like this:
Processing MusicServiceAdmin::UsersController#edit (for 127.0.0.1 at 2014-07-31 10:34:16) [GET]
Session ID: 3c0cfbe7ff23f8f718f6626748a4a
Parameters: {"id"=>"35363"}
Params
Each request in Rails is tracked using the console - you should examine the params which come through from there:
Started POST "/admin/login" for 127.0.0.1 at 2014-04-26 16:25:34 +0100
Processing by Admin::Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+I1TKfc1YDuil7EI1frrFIoerNg5uonB1CIiujJO0jo=", "user"=>{"email"=>"*********", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
[1m[36mUser Load (482.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`email` = 'rpeck#frontlineutilities.co.uk' ORDER BY `users`.`id` ASC LIMIT 1[0m
[1m[35m (435.0ms)[0m BEGIN
[1m[36mSQL (439.0ms)[0m [1mUPDATE `users` SET `current_sign_in_at` = '2014-04-26 15:25:35', `last_sign_in_at` = '2014-04-04 12:08:45', `sign_in_count` = 51, `updated_at` = '2014-04-26 15:25:36' WHERE `users`.`id` = 2[0m
[1m[35m (448.0ms)[0m COMMIT
[1m[36mOption Load (404.0ms)[0m [1mSELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_robots' LIMIT 1[0m
[1m[35mOption Load (357.0ms)[0m SELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_title' LIMIT 1
[1m[36mOption Load (428.0ms)[0m [1mSELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_description' LIMIT 1[0m
Redirected to http://lvh.me:3000/****
Completed 302 Found in 3165ms (ActiveRecord: 2993.2ms)
If you want to "debug", you just have to examine the various params / actions which are rendered in the console. Alternatively, you may also wish to the use the Rails.logger.info method for specific data:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def index
Rails.logger.info("Test")
end
end
Related
I have a small inherited rails project that uses devise to authenticate.
Recently it has started making an incorrect query to the database if the user enters an invalid password, as set out below. Previously it works as expected. I must have changed something, but I do not know what.
With a VALID password
When the user logs in with a VALID password, the console log shows similar to this
Started POST "/users/sign_in" for 192.168.2.30 at 2015-07-13 08:13:39 -0400
Processing by Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"A_Long_Authenticity_Token_Goes_Here", "user"=>{"email"=>"m.mouse#disney.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["email", "m.mouse#disney.com"]]
(0.3ms) BEGIN
SQL (0.6ms) UPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5 [["last_sign_in_at", "2015-07-10 21:17:12.592611"], ["current_sign_in_at", "2015-07-13 12:13:39.359997"], ["sign_in_count", 1000], ["updated_at", "2015-07-13 12:13:39.363621"], ["id", 22]]
(17.6ms) COMMIT
and the system carries on as normal.
With an INvalid password
When the user attempts to log in with an INVALID password, the console log shows similar to this
Started POST "/users/sign_in" for 192.168.2.30 at 2015-07-13 07:44:55 -0400
Processing by Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"A_Long_Authenticity_Token_Goes_Here", "user"=>{"email"=>"m.mouse#disney.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (53.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["email", "m.mouse#disney.com"]]
Completed 401 Unauthorized in 288ms (ActiveRecord: 53.4ms)
Processing by Users::SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"A_Long_Authenticity_Token_Goes_Here", "user"=>{"email"=>"m.mouse#disney.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "email"."email" = 'm.mouse#disney.com' AND "email"."password" = 'ThisIsAnInvalidPassword' AND "email"."remember_me" = '0' ORDER BY "users"."id" ASC LIMIT 1
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "email"
LINE 1: SELECT "users".* FROM "users" WHERE "email"."email" = 'm.mou....
: SELECT "users".* FROM "users" WHERE "email"."email" = 'm.mouse#disney.com' AND "email"."password" = 'ThisIsAnInvalidPassword' AND "email"."remember_me" = '0' ORDER BY "users"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 19ms (ActiveRecord: 3.3ms)
<< Output standard rails error page >>
So far as I understand this, the system tries to read the user table as expected, but no row is found. Devise munges this into a 401 Unauthorized response. The system is then attempting to redirect back to the login page somehow using Users::SessionsController#new
The system then tries a completely new query trying to look up the user using a half formatted query. The query tries to include a table called email that does not exist in the database; the query syntax is not correct either.
Database: postgres
Rails: 2.1.2
Devise gem: 3.5.1 according to bundle show
There is no Users::SessionsController#create def, so presumably using the underlying devise version
There is a Users::SessionsController#new as follows
def new
if (Rails.env.development? || Rails.env.test?) && params[:user]
user = User.where(email: params[:user]).first
sign_in :user, user
redirect_to dashboard_home_path
else
super
end
end
The environment is development
Nothing appears to be being written to the sessions table whether the login is successful or not.
Where does that second malformed query even come from, and why does devise try to use/call Users::SessionsController#new after an invalid login attempt anyway?
Thanks in advance
I have Ruby on Rails 4 application also I am using Devise gem to provide login/register.
Everything works just fine, but users that are logged in can't delete their own account. Any other delete functions work, so Javascript shouldn't be a problem.
In view:
<%= link_to "Delete account", registration_path(resource_name), data: { confirm: "Your profile will be permanently deleted. Are you sure?" }, method: :delete%>
I am overriding default devise Registration controller with this:
class RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
My log file:
Started DELETE "/ru/users" for xx.xxx.xxxx.xx at 2015-05-26 22:30:45 +0300
Processing by RegistrationsController#destroy as HTML
Parameters: {"authenticity_token"=>"dXKjMMmKibIy2LNCrR66JuThjPzqsdtjFMXTCubcFe8=", "locale"=>"ru"}
[1m[36mUser Load (0.3ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 66 ORDER BY `users`.`id` ASC LIMIT 1[0m
[1m[35mCountry Load (0.3ms)[0m SELECT `countries`.* FROM `countries` WHERE `countries`.`id` = 1 LIMIT 1
[1m[36m (0.6ms)[0m [1mSELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (vip_recomend >= '2015-05-26 22:30:45')[0m
[1m[35m (0.4ms)[0m SELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (recomend >= '2015-05-26 22:30:45')
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (highlight >= '2015-05-26 22:30:45')[0m
[1m[35m (0.3ms)[0m SELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (vip_highlight >= '2015-05-26 22:30:45')
[1m[36mRegion Load (0.2ms)[0m [1mSELECT `regions`.* FROM `regions` WHERE `regions`.`country_id` = 1[0m
[1m[35mList Load (0.3ms)[0m SELECT `lists`.* FROM `lists` WHERE (billing_id =5) ORDER BY `lists`.`id` ASC LIMIT 1
[1m[36m (0.3ms)[0m [1mBEGIN[0m
[1m[35m (0.2ms)[0m COMMIT
[1m[36m (0.2ms)[0m [1mBEGIN[0m
[1m[35m (0.2ms)[0m COMMIT
Redirected to http://xxxxxxxxxxx.xxx.xxx/ru
Completed 302 Found in 24ms (ActiveRecord: 4.4ms)
Description:
When user pushes delete button, user gets message :
translation missing: ru.devise.registrations.user.destroyed
also he is logged out from his account, but the actual destroying never happens. What could be possible problem?
Thanks for your time.
Best wishes.
Here Destroy deletes user account by using destroy method of devise registrations_controller. For translation missing: ru.devise.registrations.user.destroyed you need to make changes in devise.ru.yml file -
ru:
devise:
registrations:
user:
destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
This is background process. So, you can't see in your logs(delete sql query).
I get the error
SocketError in UsersController#create
getaddrinfo: nodename nor servname provided, or not known
When i try to crate an account in my rails app. I think that the issue relates either to ejabberd or action mailer. How can I find out more about where this error comes from?
Here is def create in my users_controller
def create
#user = User.new(params[:user])
if #user.save!
clear_current_session
MailAgent.registration_confirmation(#user).deliver
flash[:notice] = 'Please activate your account by clicking on the link sent to you by email.'
redirect_to new_user_url
else
flash.now[:error] = 'Please fix the listed errors'
render 'new'
end
end
Here is the full error
Started GET "/" for 127.0.0.1 at 2011-08-02 10:18:33 +1000
Processing by PagesController#home as HTML
Redirected to http://localhost:3000/users/new
Completed 302 Found in 0ms
Started GET "/users/new" for 127.0.0.1 at 2011-08-02 10:18:33 +1000
Processing by UsersController#new as HTML
Rendered users/_signup_form.html.erb (6.5ms)
Rendered layouts/_login_links.html.erb (23.3ms)
Rendered layouts/_error_messages.html.erb (0.3ms)
Rendered users/new.html.erb within layouts/application (33.7ms)
Completed 200 OK in 36ms (Views: 35.6ms | ActiveRecord: 0.2ms)
Started POST "/users" for 127.0.0.1 at 2011-08-02 10:18:49 +1000
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0JgPvOiu18OjD6TmpUrxDGtPiTUBlw0W78HqtdV/brc=", "user"=>{"name"=>"test", "email"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
[1m[36mSQL (0.1ms)[0m [1mBEGIN[0m
[1m[35mUser Load (0.1ms)[0m SELECT `users`.`id` FROM `users` WHERE (LOWER(`users`.`email`) = LOWER("[FILTERED]")) LIMIT 1
[1m[36mUser Load (0.1ms)[0m [1mSELECT `users`.`id` FROM `users` WHERE (`users`.`persistence_token` = BINARY '61b972eee6ed6eb2341b08d92640b08e6379b8743a4df5aa73a233151a6d5629896470e00e108d0f08d20814fb67d8') LIMIT 1[0m
[1m[35mSQL (1.7ms)[0m describe `users`
[1m[36mAREL (0.2ms)[0m [1mINSERT INTO `users` (`name`, `email`, `crypted_password`, `password_salt`, `persistence_token`, `perishable_token`, `login_count`, `last_login_at`, `created_at`, `updated_at`, `activated`) VALUES ('test', "[FILTERED]", '37712237698ea6db7f044f9408adee9d1fe02447bdb1b92c34b71d2ca35795dd04af1528aa73b6e7db5b11f', 'JnajFzu5luNXbeU', '61b972ec6e0ee6ed6eb08e6379b8743a448ced73ddf5aa73a233151a6d56298e53a996470e00e108d0f08d20814f9c461185f0b67d8', 'bIPlmXIFPjJDHzt', 1, NULL, '2011-08-02 00:18:49', '2011-08-02 00:18:49', 0)[0m
[1m[35mJabberCredential Load (0.1ms)[0m SELECT `jabber_credentials`.* FROM `jabber_credentials` WHERE (`jabber_credentials`.user_id = 12) LIMIT 1
[1m[36mJabberCredential Load (0.1ms)[0m [1mSELECT `jabber_credentials`.`id` FROM `jabber_credentials` WHERE (`jabber_credentials`.`jabber_id` = BINARY '12_test_72') LIMIT 1[0m
[1m[35mSQL (1.3ms)[0m describe `jabber_credentials`
[1m[36mAREL (0.1ms)[0m [1mINSERT INTO `jabber_credentials` (`jabber_id`, `jabber_password`, `user_id`, `created_at`, `updated_at`) VALUES ('12_test_72', '123456', 12, '2011-08-02 00:18:49', '2011-08-02 00:18:49')[0m
[1m[35mSQL (0.5ms)[0m ROLLBACK
Completed in 32093ms
SocketError (getaddrinfo: nodename nor servname provided, or not known):
lib/xmpp_client.rb:14:in `initialize'
app/models/user.rb:27:in `new'
app/models/user.rb:27:in `register_jabber'
app/models/user_observer.rb:7:in `after_create'
app/controllers/users_controller.rb:11:in `create'
Rendered /Users/fred/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.6ms)
Rendered /Users/fred/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.4ms)
Rendered /Users/fred/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.8ms)
Check your log file, whenever an exception is thrown it outputs the stack trace. Lines closest to the top are more relevant than those lower down. Many of the lines will be "false errors" meaning they are not the problem, but fail because of the problem. It takes some guess-work and a healthy dose of experience to figure out which one is relevant.
Please include the stack trace and maybe we can help you further.
I'm working on the Ruby on Rails Tutorial. I have the asynchronous follow buttons working. Interestingly, each of the javascript calls is getting called twice in a row when I click the button. Any thoughts on how I make it only send one request? Here is my log
Started POST "/tag_user_relationships/123" for
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "commit"=>"Unfollow", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id`
= 9 LIMIT 1 TagUserRelationship Load (0.3ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`tag_id` = 9 AND (`tag_user_relationships`.user_id
= 2) LIMIT 1 SQL (0.1ms) BEGIN AREL (0.2ms) DELETE FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 SQL (0.4ms) COMMIT SQL (0.4ms) SELECT COUNT(*) FROM `users` INNER JOIN `tag_user_relationships` ON `users`.id = `tag_user_relationships`.user_id WHERE ((`tag_user_relationships`.tag_id = 9)) SQL (0.3ms) SELECT COUNT(*) FROM `tags` INNER JOIN `tag_user_relationships` ON `tags`.id
= `tag_user_relationships`.tag_id WHERE ((`tag_user_relationships`.user_id = 2)) Rendered tag_user_relationships/_form.js.erb (15.8ms) Rendered tags/_follow.html.erb (2.1ms) Rendered tag_user_relationships/destroy.js.erb (20.3ms) Completed 200 OK in 138ms (Views: 28.1ms | ActiveRecord: 2.3ms)
Started POST "/tag_user_relationships/123" for
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Completed 404 Not Found in 70ms
ActiveRecord::RecordNotFound (Couldn't find TagUserRelationship with ID=123): app/controllers/tag_user_relationships_controller.rb:14:in `destroy'
Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (153.6ms) Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (160.2ms)
And here is my view
$("#<%= "follower_info#{#tag.id}" %>").html("<%=escape_javascript(pluralize(#tag.followers.count,'follower'))%>");
var link = $('<a>').attr('href',"<%=user_tags_path(current_user) %>").text("<%= escape_javascript(pluralize(current_user.beats.count,'tag')) %>");
$("#<%= "user#{current_user.id}_following" %>").html(link); $("#<%= "follow_form#{#tag.id}" %>").html("<%= escape_javascript("#{render('tags/unfollow', :tag => #tag)}").html_safe %>");
And my controller
def create
#tag = Tag.find(params[:tag_user_relationship][:tag_id])
current_user.follow_tag!(#tag)
respond_to do |format|
format.html { redirect_to #tag }
format.js
end
end
I had this same issue recently. Is there any chance that you installed the ajax gem and also included the jquery code in the Javascripts folder? If so, the calls will be made twice.
I have the following:
class User < ActiveRecord::Base
before_update :guest_upgrade
def guest_upgrade
# If the user changed their email that means they were a guest, and are no longer.
# Likely triggered from the Registrations#Update controller
if self.email_changed?
self.guest = false
end
end
This is causing rollbacks, here is the log with the above in play:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:54:00 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"xxxxx", "lname"=>"xxxx", "email"=>"xxxxxxx#gmail.com", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Create my account", "fb_access_token"=>"", "fb_uuid"=>""}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.5ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxxxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
SQL (0.2ms) ROLLBACK
Authentication Load (0.5ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered layouts/_header.html.erb (3.9ms)
CACHE (0.0ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered registrations/edit.html.erb within layouts/application (107.0ms)
Completed 200 OK in 484ms (Views: 111.7ms | ActiveRecord: 6.0ms)
Yet if I comment out the guest_upgrade it works fine:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:55:23 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"XXXX", "lname"=>"XXXX", "email"=>"xxxx#gmail.com"}, "commit"=>"Save Changes"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.4ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
AREL (0.7ms) UPDATE "users" SET "fname" = 'XXXX', "lname" = 'XXXX', "email" = 'xxxx#gmail.com', "updated_at" = '2011-07-18 21:55:23.817142' WHERE "users"."id" = 5
[paperclip] Saving attachments.
SQL (37.9ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 780ms
Am I using the dirty objects incorrectly?
All I want to do is, when User is updated, if the user changes there email, change the user.guest field to false.
Suggestions? Thanks
I think I've had this problem before. I believe what's happening is it's returning false; try adding an explicit return like so:
if self.email_changed?
self.guest = false
return true
end
Check out the section marked Canceling Callbacks at ruby on rails.org api. It reads:
Canceling callbacks
If a before_* callback returns false, all the later callbacks and the
associated action are cancelled. If an after_* callback returns false,
all the later callbacks are cancelled. Callbacks are generally run in
the order they are defined, with the exception of callbacks defined as
methods on the model, which are called last.
I know I'm late but I hope this helps!