Search code examples
ruby-on-railsdevise

Rails - Devise's registration controller create action seems to trigger twice


I added this lines of code to create action:

def create 
    super
  @card = Card.find(params[:card_id])
  @card.update(:user_id=>current_user)
  end

And everything works fine, user gets created, card gets updated, but after redirect this happens:

Couldn't find Card with 'id'=
Extracted source (around line #14):

def create 
  super
  @card = Card.find(params[:card_id])
  @card.update(:user_id=>current_user)
 end

I checked my terminal to find out the reason why this happens, and it seems that create action triggers twice for no reason:

Started POST "/users" for ::1 at 2020-08-12 11:04:34 +0300

Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"q1W0+ZhzK85uHTcp1x4jKHvCG0ukIgj2JxZuAy6vuLQl/vPqJVu6eXSEWviYTnWC4cXAJk2xCJhl8mgoWzXIAA==", "user"=>{"name"=>"Терл Кабот", "email"=>"tafff1@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "card_id"=>"2000012606"}, "commit"=>"Sign up"}

  Card Load (1.0ms)  SELECT  "cards".* FROM "cards" WHERE "cards"."id" = $1 LIMIT $2  [["id", 2000012606], ["LIMIT", 1]]
   (0.0ms)  
BEGIN

  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "tafff1@gmail.com"], ["LIMIT", 1]]

  SQL (1.0ms)  INSERT INTO "users" ("email", "encrypted_password", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["email", "tafff1@gmail.com"], ["encrypted_password", "$2a$12$qTrv/zFUxULi9sqWgYlY/uPjQoJsZxB8PJK2ae/e6YfAFT40ci47e"], ["name", "Терл Кабот"], ["created_at", "2020-08-12 08:04:35.174621"], ["updated_at", "2020-08-12 08:04:35.174621"]]

  SQL (1.0ms)  UPDATE "cards" SET "user_id" = $1, "updated_at" = $2 WHERE "cards"."id" = $3  [["user_id", 17], ["updated_at", "2020-08-12 08:04:35.178626"], ["id", 2000012606]]

   (1.0ms)  COMMIT
Redirected to http://localhost:3000/

  Card Load (0.0ms)  SELECT  "cards".* FROM "cards" WHERE "cards"."id" = $1 LIMIT $2  [["id", nil], ["LIMIT", 1]]

Completed 404 Not Found in 378ms (ActiveRecord: 6.0ms)



ActiveRecord::RecordNotFound (Couldn't find Card with 'id'=):

is there any solution for this?

EDIT: I gave up and just changed card and user logic, now user belongs to card, so I dont have to update cards user_id from devises create action.


Solution

  • The card_id is nested in the user key, so it will be: params[:user][:card_id]