How to pass time_select in controller params? - ruby-on-rails

<%= f.time_select :exact, {ampm: true, :discard_minute=> true} %> # t.time "exact"
How do I pass the above in params?
session[:challenge_exact] = params[:exact].parse_time_select! # got nil error
session[:challenge_exact] = challenge_params[:exact]
session[:challenge_exact] = [params["challenge"]["exact(1i)"], params["challenge"]["exact(2i)"], params["challenge"]["exact(3i)"], params["challenge"]["exact(4i)"], params["challenge"]["exact(5i)"]].join(',')
None of the above work. After the challenge is created I see that exact: nil even though it was populated in the form.
Started POST "/challenges" for ::1 at 2017-05-02 08:28:31 -0400
Processing by ChallengesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oN+DzJK+/GGmpTOOVYMUJQdywuENwxhmX/EVeW6NSZ3w1MctoMd/VHS39d+JasEjHujO88nLPSB2qUKEhet5yA==", "challenge"=>{"name"=>"Write 3 Gratitudes", "categorization"=>"health", "category"=>"goal", "deadline(2i)"=>"6", "deadline(3i)"=>"2", "deadline(1i)"=>"2017", "push"=>"0", "message"=>"1", "mail"=>"0", "remind"=>["mon", "thu", ""], "exact(1i)"=>"2017", "exact(2i)"=>"5", "exact(3i)"=>"2", "exact(4i)"=>"08", "exact(5i)"=>"27", "conceal"=>"0"}, "number"=>"", "email"=>"", "button"=>""}
Redirected to http://localhost:3000/signup
Completed 302 Found in 47ms (ActiveRecord: 0.0ms)
After POST challenge a person then creates a user account...
Started POST "/users" for ::1 at 2017-05-03 01:02:13 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4Lv5NSfH9tMnC9+t5bVC5x1d571y0jhwsgvYU3t2sp85XAnNOsHk+FCmtGcj2wJVysV6IlAIzF4jCx1hIofwsNg==", "user"=>{"name"=>"test", "last_name"=>"daddy", "email"=>"testdaddy#gmail.com", "password"=>"[FILTERED]", "time_zone"=>"Eastern Time (US & Canada)", "subscribe"=>"0"}, "button"=>""}
(0.2ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('testdaddy#gmail.com') LIMIT 1
SQL (2.3ms) INSERT INTO "users" ("time_zone", "name", "last_name", "subscribe", "email", "password_digest", "number", "created_at", "updated_at", "activation_digest", "activated") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["time_zone", "Eastern Time (US & Canada)"], ["name", "test"], ["last_name", "daddy"], ["subscribe", "f"], ["email", "testdaddy#gmail.com"], ["password_digest", "$2a$10$JSSPUl0JDnUXybnAuSnEO.q5DgZscaU3hRpcM4C5L0a0aEtflzb7ne"], ["number", "15169499507"], ["created_at", "2017-05-03 05:02:14.144232"], ["updated_at", "2017-05-03 05:02:14.144232"], ["activation_digest", "$2a$10$NDtWKvxuJm/Hb9YIOzuVIuPpttCZwjsgYws6HhEcbHc.PukxXNpeLa"], ["activated", "t"]]
(18.8ms) COMMIT
(0.2ms) BEGIN
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 16]]
# AS YOU CAN SEE WITH INSERT INTO CHALLENGES FOR SOME REASON THE SESSION PARAMS FOR EXACT ISN'T PASSING FOR EXACT
SQL (0.6ms) INSERT INTO "challenges" ("committed", "name", "categorization", "category", "deadline", "remind", "message", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["committed", nil], ["name", "Write 3 Gratitudes"], ["categorization", "health"], ["category", "goal"], ["deadline", "2017-06-03"], ["remind", "---\n- mon\n- thu\n- ''\n"], ["message", "t"], ["user_id", 16], ["created_at", "2017-05-03 05:02:14.434251"], ["updated_at", "2017-05-03 05:02:14.434251"]]
ActsAsTaggableOn::Tag Load (0.6ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 40], ["taggable_type", "Challenge"]]
ActsAsTaggableOn::Tag Load (0.8ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND
taggings.tagger_id = 16 AND
taggings.tagger_type = 'User') [["taggable_id", 40], ["taggable_type", "Challenge"]]
(17.2ms) COMMIT
Redirected to http://localhost:3000/
How it appears in console...
exact: 2000-01-01 02:32:00 UTC>

You can access exact params like:
> params[:challenge]["exact(1i)"]
#=> "2017"
> params[:challenge]["exact(2i)"]
#=> "5"
> params[:challenge]["exact(3i)"]
#=> "2"
> params[:challenge]["exact(4i)"]
#=> "08"
> params[:challenge]["exact(5i)"]
#=> "27"
You can parse time like this:
exact_time = "#{params[:challenge]['exact(4i)']}:#{params[:challenge]['exact(5i)']}"
#=> "08:27"
> session[:challenge_exact] = Time.parse(exact_time)
#=> 2017-05-03 08:27:00 +0530

I would do:
session[:challenge_exact] = components_to_absolute(params[:exact])
def components_to_absolute(components)
DateTime.new(components['exact(1i)'].to_i,
components['exact(2i)'].to_i,
components['exact(3i)'].to_i,
components['exact(4i)'].to_i,
components['exact(5i)'].to_i)
end

Related

Using Papertrail's attribute_name_was

I have a request that makes several changes to the same record, however I only need to store a version when one of the attributes changes from false to true. I tried the following:
has_paper_trail on: [:update],
only: {
redone: Proc.new { |srv|
srv.redone == true && srv.redone_was == false
},
}
But I am still getting versions on changes that do not involve the redone attribute changing from false to true... This is an excerpt from the log:
(0.7ms) BEGIN
SQL (11.3ms) UPDATE "servicings" SET "redone" = $1, "redo_reason" = $2, "updated_at" = $3 WHERE "servicings"."id" = $4 [["redone", false], ["redo_reason", nil], ["updated_at", 2018-07-19 22:46:15 UTC], ["id", 218762]]
SQL (9.7ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:14:11.143007000 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: open\napproval_status: \nredone: true\nunable_reason: \nredo_reason: testing paper_trail\napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\nredone:\n- true\n- false\nredo_reason:\n- testing paper_trail\n- \nupdated_at:\n- 2018-07-19 22:14:11.143007000 Z\n- 2018-07-19 22:46:15.680447100 Z\n"], ["created_at", 2018-07-19 22:46:15 UTC]]
(5.6ms) COMMIT
(2.0ms) BEGIN
(0.8ms) COMMIT
(1.5ms) BEGIN
SQL (9.1ms) UPDATE "servicings" SET "updated_at" = $1, "status" = $2 WHERE "servicings"."id" = $3 [["updated_at", 2018-07-19 22:46:15 UTC], ["status", "completed"], ["id", 218762]]
SQL (2.8ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nredone: false\nredo_reason: \nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:46:15.680447100 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: open\napproval_status: \nunable_reason: \napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\nupdated_at:\n- 2018-07-19 22:46:15.680447100 Z\n- 2018-07-19 22:46:15.981721300 Z\nstatus:\n- open\n- completed\n"], ["created_at", 2018-07-19 22:46:15 UTC]]
(8.7ms) COMMIT
WorkOrder Load (15.1ms) SELECT "work_orders".* FROM "work_orders" WHERE "work_orders"."deleted_at" IS NULL AND "work_orders"."id" = $1 LIMIT $2 [["id", 7462], ["LIMIT", 1]]
(1.2ms) BEGIN
SQL (16.3ms) UPDATE "servicings" SET "completed_at" = $1, "updated_at" = $2 WHERE "servicings"."id" = $3 [["completed_at", 2018-07-19 22:46:16 UTC], ["updated_at", 2018-07-19 22:46:16 UTC], ["id", 218762]]
SQL (7.5ms) INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["item_type", "Servicing"], ["item_id", 218762], ["event", "update"], ["object", "---\nid: 218762\nredone: false\nredo_reason: \nwork_order_id: 7462\narea_id: 16563\nhigh_priority: false\ncompleted_at: \ncreated_at: 2018-07-10 19:45:36.245612000 Z\nupdated_at: 2018-07-19 22:46:15.981721300 Z\nassignee_id: 90\nclaimed_by_id: \ncompleted_by_id: 90\noccupied_at: \ndeleted_at: \nstatus: completed\napproval_status: \nunable_reason: \napproving_user_id: \napproving_user_name: \napproved_at: \n"], ["object_changes", "---\ncompleted_at:\n- \n- 2018-07-19 22:46:16.111954100 Z\nupdated_at:\n- 2018-07-19 22:46:15.981721300 Z\n- 2018-07-19 22:46:16.150183500 Z\n"], ["created_at", 2018-07-19 22:46:16 UTC]]
(2.7ms) COMMIT
What could I be doing wrong?
It's probably because PaperTrail's callback executes after the update. If that's the case, redone_was will always be equal to the current redone value.
You should store your redone_was value somewhere before updating.
class Klass
attr_accessor :old_redone_value
before_save :store_old_redone_value
has_paper_trail on: [:update],
only: {
redone: Proc.new { |srv|
srv.redone == true && srv.old_redone_value == false
},
}
def store_old_redone_value
self.old_redone_value = redone_was
end
end
That's the idea. It's not the best solution, and might not work -- but, it'll lead you somewhere.

Setting value as params on creation

Attempting to make different orders where depending on the order there is different revisions_left which is an integer in the migration file.
I pass the params[:orderref] through a button like so <%= link_to 'Package 1', new_order_path(orderref: 1)%> and then am trying to call it in the create method like so
if params[:orderref] == "1"
#order.revisions_left = 1
elsif params[:orderref] == "2"
#order.revisions_left = 2
elsif params[:orderref] == "3"
#order.revisions_left = 3
end
However the revisions_left fails to save and am unsure why
Edit: Logs
Processing by OrderController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VGt/2PCPMmVnzXGm+UgHcmSvX7FEHxOoF/TfJoKf8gn3NJ0grD9JkydfswwV50GXIneFuTSClLK7cqvpsRCwRg==", "order"=>{"name"=>"sdada", "email"=>"as#tt.com", "company"=>"", "event_type"=>"Birthday", "country"=>"Brazil", "description"=>"1233 1 123 11", "order_type"=>"3", "price"=>"80"}, "commit"=>"Create Order"}
Order Load (3.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."name" = $1 AND "orders"."company" = $2 AND "orders"."email" = $3 AND "orders"."event_type" = $4 AND "orders"."country" = $5 AND "orders"."description" = $6 AND "orders"."order_type" = $7 AND "orders"."price" = $8 ORDER BY "orders"."id" ASC LIMIT $9 [["name", "sdada"], ["company", ""], ["email", "as#tt.com"], ["event_type", "Birthday"], ["country", "Brazil"], ["description", "1233 1 123 11"], ["order_type", 3], ["price", 80], ["LIMIT", 1]]
(0.4ms) BEGIN
Order Exists (1.0ms) SELECT 1 AS one FROM "orders" WHERE LOWER("orders"."email") = LOWER($1) LIMIT $2 [["email", "as#tt.com"], ["LIMIT", 1]]
SQL (6.2ms) INSERT INTO "orders" ("name", "company", "email", "event_type", "country", "description", "price", "order_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["name", "sdada"], ["company", ""], ["email", "as#tt.com"], ["event_type", "Birthday"], ["country", "Brazil"], ["description", "1233 1 123 11"], ["price", 80], ["order_type", 3], ["created_at", 2017-08-03 05:26:21 UTC], ["updated_at", 2017-08-03 05:26:21 UTC]]
(71.2ms) COMMIT
(0.1ms) BEGIN
Order Exists (0.5ms) SELECT 1 AS one FROM "orders" WHERE LOWER("orders"."email") = LOWER($1) AND ("orders"."id" != $2) LIMIT $3 [["email", "as#tt.com"], ["id", 3], ["LIMIT", 1]]
SQL (0.7ms) UPDATE "orders" SET "status" = $1, "updated_at" = $2 WHERE "orders"."id" = $3 [["status", 1], ["updated_at", 2017-08-03 05:26:22 UTC], ["id", 3]]
(0.3ms) COMMIT

rails devise - authentication google - can create but not login

When I create a new account using google authentication, it creates a account and also do the login automaticaly.
If I click logout and later login again, with the same google account, I can't login...
I get no error message :(
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, event: :authentication
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def google_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, event: :authentication
set_flash_message(:notice, :success, kind: "Google") if is_navigational_format?
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Another informations.
Here is the log when the autentication / account creation works:
Started GET "/auth/google_oauth2" for 177.207.232.141 at 2016-10-23 23:47:31 +0000
Started GET "/auth/google_oauth2/callback?state=1f3b3124e0264c1dabb3f40059d9a15a9da524553e0ec5dc&code=4/ZjqBwsoXJJXncItmMDuwPssw0ooT1RwnNjkRIfilQYc" for 177.207.232.141 at 2016-10-23 23:47:33 +0000
Processing by OmniauthCallbacksController#google_oauth2 as HTML
Parameters: {"state"=>"1f3b3124e0264c1dabb3f40059d9a15a9da524553e0ec5dc", "code"=>"4/ZjqBwsoXJJXncItmMDuwPssw0ooT1RwnNjkRIfilQYc"}
[1m[35mSubdomain Load (27.7ms)[0m SELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1 [["address", "yoga"]]
[1m[36mDomain Load (27.5ms)[0m [1mSELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1[0m [["address", "smartmarket.io"]]
[1m[35mUser Load (28.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]]
[1m[36m (27.7ms)[0m [1mBEGIN[0m
[1m[35mUser Exists (27.8ms)[0m SELECT 1 AS one FROM "users" WHERE "users"."email" = 'diogowernik#gmail.com' LIMIT 1
[1m[36mUser Exists (27.8ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'diogowernik#gmail.com' LIMIT 1[0m
[1m[35mSQL (27.9ms)[0m INSERT INTO "users" ("provider", "uid", "name", "email", "image", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["provider", "google_oauth2"], ["uid", "really?"], ["name", "Diogo Wernik"], ["email", "diogowernik#gmail.com"], ["image", "https://lh6.googleusercontent.com/-vXjHRteu8BY/AAAAAAAAAAI/AAAAAAAAB3A/_oEJPVq8h8o/photo.jpg"], ["encrypted_password", "$2a$10$6CIsOntiZxoLQfyUTfskiuys3P4u6.M0O4h/4S5zoW7EpN2KgX4Re"], ["created_at", "2016-10-23 23:47:34.023034"], ["updated_at", "2016-10-23 23:47:34.023034"]]
[1m[36mSQL (28.1ms)[0m [1mINSERT INTO "profiles" ("name", "kind", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m [["name", "Diogo Wernik"], ["kind", "blog"], ["user_id", 5], ["created_at", "2016-10-23 23:47:34.082199"], ["updated_at", "2016-10-23 23:47:34.082199"]]
[1m[35mSQL (28.4ms)[0m INSERT INTO "user_main_profiles" ("profile_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["profile_id", 5], ["user_id", 5], ["created_at", "2016-10-23 23:47:34.140877"], ["updated_at", "2016-10-23 23:47:34.140877"]]
[1m[36mUserMainProfile Load (27.7ms)[0m [1mSELECT "user_main_profiles".* FROM "user_main_profiles" WHERE "user_main_profiles"."user_id" = $1 LIMIT 1[0m [["user_id", 5]]
[1m[35m (28.1ms)[0m COMMIT
[1m[36mUser Load (27.9ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
[1m[35m (27.5ms)[0m BEGIN
[1m[36mSQL (27.9ms)[0m [1mUPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "last_sign_in_ip" = $3, "current_sign_in_ip" = $4, "sign_in_count" = $5, "updated_at" = $6 WHERE "users"."id" = $7[0m [["last_sign_in_at", "2016-10-23 23:47:34.301029"], ["current_sign_in_at", "2016-10-23 23:47:34.301029"], ["last_sign_in_ip", "177.207.232.141/32"], ["current_sign_in_ip", "177.207.232.141/32"], ["sign_in_count", 1], ["updated_at", "2016-10-23 23:47:34.329926"], ["id", 5]]
[1m[35m (28.0ms)[0m COMMIT
Redirected to http://yoga.smartmarket.io/
Completed 302 Found in 636ms (ActiveRecord: 418.1ms)
[1m[36m (27.5ms)[0m [1mBEGIN[0m
[1m[35mSQL (27.7ms)[0m DELETE FROM "sessions" WHERE "sessions"."id" = $1 [["id", 11]]
[1m[36m (27.9ms)[0m [1mCOMMIT[0m
Here is when i logout and try to login again with the same account
Started GET "/auth/google_oauth2" for 177.207.232.141 at 2016-10-23 23:51:13 +0000
Started GET "/auth/google_oauth2/callback?state=ca339595a33e9e192c274a78450400a02e7edfd7f7ebb937&code=4/bQ59Fm5Fcxv_vWtBlFpMAwMoY6iig8J6bZGWFKboZms" for 177.207.232.141 at 2016-10-23 23:51:14 +0000
Processing by OmniauthCallbacksController#google_oauth2 as HTML
Parameters: {"state"=>"ca339595a33e9e192c274a78450400a02e7edfd7f7ebb937", "code"=>"4/bQ59Fm5Fcxv_vWtBlFpMAwMoY6iig8J6bZGWFKboZms"}
[1m[35mSubdomain Load (27.6ms)[0m SELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1 [["address", "yoga"]]
[1m[36mDomain Load (27.5ms)[0m [1mSELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1[0m [["address", "smartmarket.io"]]
[1m[35mUser Load (27.9ms)[0m SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]]
[1m[36m (27.7ms)[0m [1mBEGIN[0m
[1m[35mUser Exists (28.5ms)[0m SELECT 1 AS one FROM "users" WHERE "users"."email" = 'diogowernik#gmail.com' LIMIT 1
[1m[36mUser Exists (28.0ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'diogowernik#gmail.com' LIMIT 1[0m
[1m[35m (27.8ms)[0m ROLLBACK
Redirected to http://yoga.smartmarket.io/sign_up
Completed 302 Found in 271ms (ActiveRecord: 195.0ms)
Started GET "/sign_up" for 177.207.232.141 at 2016-10-23 23:51:15 +0000
Processing by Devise::RegistrationsController#new as HTML
[1m[36mSubdomain Load (27.5ms)[0m [1mSELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1[0m [["address", "yoga"]]
[1m[35mDomain Load (27.7ms)[0m SELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1 [["address", "smartmarket.io"]]
Rendered devise/registrations/_social.erb (0.5ms)
Rendered devise/registrations/new.html.erb within layouts/application (2.0ms)
Rendered layouts/partials/_topnavbar.html.erb (0.4ms)
Rendered layouts/partials/_sidebar.html.erb (0.1ms)
Rendered layouts/partials/_footer.html.erb (0.0ms)
Rendered layouts/partials/_alerts.erb (0.1ms)
Completed 200 OK in 251ms (Views: 165.2ms | ActiveRecord: 83.0ms)
Started GET "/api/i18n/site-pt.json" for 177.207.232.141 at 2016-10-23 23:51:16 +0000
Processing by ApiController#i18n as JSON
Parameters: {"locale"=>"site-pt"}
Rendered app/assets/i18n/site-pt.json (0.1ms)
Completed 200 OK in 35ms (Views: 5.8ms | ActiveRecord: 27.9ms)
Started GET "/assets/fontawesome/fonts/fontawesome-webfont.woff2?v=4.5.0" for 177.207.232.141 at 2016-10-23 23:51:16 +0000
Started GET "/sign_up" for 177.207.232.141 at 2016-10-23 23:51:16 +0000
Processing by Devise::RegistrationsController#new as HTML
[1m[36mSubdomain Load (27.6ms)[0m [1mSELECT "subdomains".* FROM "subdomains" WHERE "subdomains"."address" = $1 LIMIT 1[0m [["address", "yoga"]]
[1m[35mDomain Load (27.5ms)[0m SELECT "domains".* FROM "domains" WHERE "domains"."address" = $1 LIMIT 1 [["address", "smartmarket.io"]]
Rendered devise/registrations/_social.erb (0.5ms)
Rendered devise/registrations/new.html.erb within layouts/application (2.2ms)
Rendered layouts/partials/_topnavbar.html.erb (0.4ms)
Rendered layouts/partials/_sidebar.html.erb (0.1ms)
Rendered layouts/partials/_footer.html.erb (0.0ms)
Rendered layouts/partials/_alerts.erb (0.1ms)
Completed 200 OK in 231ms (Views: 144.7ms | ActiveRecord: 83.0ms)
Here is the user.rb
class User < ActiveRecord::Base
include Userable
# include DeviseTokenAuth::Concerns::User
# Include default devise modules. Others available are:
# :lockable, :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :omniauthable # , :confirmable
validates :email, uniqueness: true
enum permission: [:guest, :admin]
after_create :create_profile!, :send_welcome_email!
before_validation :set_uid!
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.name = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0, 20]
end
end
def main_profile
user_main_profile.try :profile
end
private
def set_uid!
self.uid = 'really?'
end
def create_profile!
profile = profiles.create(
name: name,
kind: 'blog'
)
create_user_main_profile(profile_id: profile.id)
end
def send_welcome_email!
UserMailer.delay.welcome(self.id)
end
end
What I can tell is that your INSERT:
[1m[35mSQL (27.9ms)[0m INSERT INTO "users" ("provider", "uid", "name", "email", "image", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["provider", "google_oauth2"], ["uid", "really?"], ["name", "Diogo Wernik"], ["email", "diogowernik#gmail.com"], ["image", "https://lh6.googleusercontent.com/-vXjHRteu8BY/AAAAAAAAAAI/AAAAAAAAB3A/_oEJPVq8h8o/photo.jpg"], ["encrypted_password", "$2a$10$6CIsOntiZxoLQfyUTfskiuys3P4u6.M0O4h/4S5zoW7EpN2KgX4Re"], ["created_at", "2016-10-23 23:47:34.023034"], ["updated_at", "2016-10-23 23:47:34.023034"]]
is passing field ["uid", "really?"] when it should be passing ["uid", "110174173081848856467"]
So when the user tries to log via Google, it searches:
SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "google_oauth2"], ["uid", "110174173081848856467"]]
Check your user.rb to see how omniauth is getting the variables or edit your post with the content.

403 Forbidden with Doorkeeper, Devise, Rails 5.0.0

I'm trying to get OAuth 2 to work with Rails 5.0.0,
I get a 403 error when test credential with rspec as following.
Can anyone please guide me if I am missing something?
I use the gems:
doorkeeper 3.1.0
postgresql 0.18.4
devise 4.1.1
rspec 3.5.0
credentials_controller.rb:
module Api
module V1
# Credentials Controller
class CredentialsController < ApiController
before_action :doorkeeper_authorize!
respond_to :json
def me
respond_with current_resource_owner
end
end
end
end
credentials_controller_spec.rb:
describe Api::V1::CredentialsController, type: :controller do
describe 'GET #me (integrated)' do
# sample
let!(:application) { Doorkeeper::Application.create!(:name => 'MyApp', :redirect_uri => 'http://app.com') }
let!(:user) { User.create!(:email => 'ax#b.com', :password => 'abc123', :password_confirmation => 'abc123') }
let!(:token) { Doorkeeper::AccessToken.create! :application_id => application.id, :resource_owner_id => user.id }
context 'When responds succeeded' do
subject(:response) { get :me, format: :json, access_token: token.token }
it 'Return 200 status code' do
expect(response.status).to be == 200
end
end
end
end
log
[1m[36mActiveRecord::SchemaMigration Load (1.1ms)[0m [1m[34mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35m (0.4ms)[0m [1m[35mBEGIN[0m
[1m[35m (0.6ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mDoorkeeper::Application Exists (2.4ms)[0m [1m[34mSELECT 1 AS one FROM "oauth_applications" WHERE "oauth_applications"."uid" = $1 LIMIT $2[0m [["uid", "e3f3a2ef90419eee24bfa1655fea0e2c8555a79fd95f94c841baa9fa07d28dfe"], ["LIMIT", 1]]
[1m[35mSQL (1.1ms)[0m [1m[32mINSERT INTO "oauth_applications" ("name", "uid", "secret", "redirect_uri", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["name", "MyApp"], ["uid", "e3f3a2ef90419eee24bfa1655fea0e2c8555a79fd95f94c841baa9fa07d28dfe"], ["secret", "1baa29493585232c23b7d53b418b6bff9df9a5f5f018a47a223f7afa6e678004"], ["redirect_uri", "http://app.com"], ["created_at", 2016-07-06 03:43:01 UTC], ["updated_at", 2016-07-06 03:43:01 UTC]]
[1m[35m (0.2ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.6ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mUser Exists (10.7ms)[0m [1m[34mSELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2[0m [["email", "ax#b.com"], ["LIMIT", 1]]
[1m[35mSQL (1.0ms)[0m [1m[32mINSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"[0m [["email", "ax#b.com"], ["encrypted_password", "$2a$11$wclAIfVHRR2rKPPMNY4By.iSEeM.xEt/9xn6ZMdIOXGWKHE8A4Kyy"], ["created_at", 2016-07-06 03:43:02 UTC], ["updated_at", 2016-07-06 03:43:02 UTC]]
[1m[35m (0.2ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.6ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mDoorkeeper::Application Load (1.2ms)[0m [1m[34mSELECT "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."id" = $1 LIMIT $2[0m [["id", 88], ["LIMIT", 1]]
[1m[36mDoorkeeper::AccessToken Exists (4.2ms)[0m [1m[34mSELECT 1 AS one FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."token" = $1 LIMIT $2[0m [["token", "2260761b3f5587619ea0944a09623624dbb1dc2d5ed539b705d6334b764a27e2"], ["LIMIT", 1]]
[1m[35mSQL (2.6ms)[0m [1m[32mINSERT INTO "oauth_access_tokens" ("resource_owner_id", "application_id", "token", "created_at") VALUES ($1, $2, $3, $4) RETURNING "id"[0m [["resource_owner_id", 354], ["application_id", 88], ["token", "2260761b3f5587619ea0944a09623624dbb1dc2d5ed539b705d6334b764a27e2"], ["created_at", 2016-07-06 03:43:02 UTC]]
[1m[35m (0.5ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
DEPRECATION WARNING: ActionController::TestCase HTTP request methods will accept only
keyword arguments in future Rails versions.
Examples:
get :show, params: { id: 1 }, session: { user_id: 1 }
process :update, method: :post, params: { id: 1 }
(called from block (4 levels) in <top (required)> at /Users/yongwoon_kim/Desktop/yongwoon/Source/00_private/ruby/ruby_rails_api_cloud_music/spec/app/controllers/v1/credentials_controller_spec.rb:16)
DEPRECATION WARNING: ActionController::TestCase HTTP request methods will accept only
keyword arguments in future Rails versions.
Examples:
get :show, params: { id: 1 }, session: { user_id: 1 }
process :update, method: :post, params: { id: 1 }
(called from block (4 levels) in <top (required)> at /Users/yongwoon_kim/Desktop/yongwoon/Source/00_private/ruby/ruby_rails_api_cloud_music/spec/app/controllers/v1/credentials_controller_spec.rb:16)
Processing by Api::V1::CredentialsController#me as JSON
Parameters: {"access_token"=>"[FILTERED]"}
[1m[36mDoorkeeper::AccessToken Load (0.2ms)[0m [1m[34mSELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."token" = $1 LIMIT $2[0m [["token", "2260761b3f5587619ea0944a09623624dbb1dc2d5ed539b705d6334b764a27e2"], ["LIMIT", 1]]
Filter chain halted as :doorkeeper_authorize! rendered or redirected
Completed 403 Forbidden in 15ms (ActiveRecord: 0.2ms)
[1m[35m (0.3ms)[0m [1m[31mROLLBACK[0m

has_many through Rails association AngularJS 'POST' $resource, how to pass in array?

I am trying to pass in an array through AngularJS ngResource 'POST' but having trouble with it. ngResource doesn't seem to like arrays being passed in through params? Is there any way to get round this so the correct associations will be made?
Controller
ctrl.recipe = new RecipeService();
ctrl.ingredients = IngredientService.query();
ctrl.recipe.carbs = 0;
ctrl.recipe.fat = 0;
ctrl.recipe.protein = 0;
ctrl.recipe.ingredient_ids = [];
ctrl.addRecipe = function() {
if (Auth.isAuthenticated()) {
ctrl.recipe.$save(function() {
var action = "created"
alerts.recipeSuccessAlert(action);
$location.path('recipes');
});
} else {
alerts.loginAlert();
};
};
ctrl.addIngredient = function(ingredient) {
ctrl.recipe.ingredient_ids.push(ingredient.id.toString());
console.log(ctrl.recipe.ingredient_ids)
};
Strong Params Rails
def recipe_params
params.require(:recipe).permit(:title, :serves, :method, :time, :carbs, :fat, :protein, :meal_type, :user_id, :calories, ingredient_ids: [])
end
Output
Started POST "/api/v1/recipes.json" for ::1 at 2016-04-29 12:19:52 +1000
Processing by Api::V1::RecipesController#create as JSON
Parameters: {"carbs"=>0, "fat"=>0, "protein"=>2, "ingredient_ids"=>["3", "2"], "title"=>"jklsad", "serves"=>4, "meal_type"=>"Breakfast", "method"=>"jkasljasdk", "time"=>"5", "calories"=>8, "recipe"=>{"title"=>"jklsad", "serves"=>4, "method"=>"jkasljasdk", "time"=>"5", "carbs"=>0, "fat"=>0, "protein"=>2, "meal_type"=>"Breakfast", "calories"=>8}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.4ms) BEGIN
SQL (0.4ms) INSERT INTO "recipes" ("title", "serves", "method", "time", "carbs", "fat", "protein", "meal_type", "calories", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["title", "jklsad"], ["serves", 4], ["method", "jkasljasdk"], ["time", 5], ["carbs", 0.0], ["fat", 0.0], ["protein", 2.0], ["meal_type", "Breakfast"], ["calories", 8.0], ["user_id", 1], ["created_at", "2016-04-29 02:19:52.029494"], ["updated_at", "2016-04-29 02:19:52.029494"]]
(7.6ms) COMMIT
Completed 200 OK in 17ms (Views: 0.6ms | ActiveRecord: 8.7ms)

Resources