Question

When a user signs up for my site, they automatically hit a signup wizard (using the WickedWizard gem). The information they input creates a Finances model, which belongs_to the User model. The User model has_one Finance.

Currently when a user submits the first Finances form, the page refreshes without any update. When I look at my logs, I see this:

 User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
  Finance Load (0.1ms)  SELECT "finances".* FROM "finances" WHERE "finances"."user_id" = ? ORDER BY "finances"."id" ASC LIMIT 1  [["user_id", 2]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
   (0.0ms)  begin transaction
   (0.1ms)  rollback transaction

Here's the Finances signup wizard controller:

  def show
    if @finance.nil?
      @finance = current_user.build_finance
    else
      @finance = Finance.find_by_user_id current_user[:id]
        end
    render_wizard
  end

  def update
    @finance = current_user.build_finance
    @finance.update_attributes(finance_params)
    render_wizard @finance
  end

I also want to take into account the use case that a User should be able to go through the wizard a second time and see its data from the Finance model it is associated with — which is why I thought the if @finance.nil? check was required in the show.

Edit:

Current the Finance model is being created, but it's not being associated with the user.

Was it helpful?

Solution

Try changing your code to:

def show
  @finance = current_user.finance || current_user.build_finance
  render_wizard
end

def update
  @finance = current_user.finance || current_user.build_finance
  @finance.assign_attributes(finance_params)
  render_wizard @finance
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top