0

当用户注册我的网站时,他们会自动点击注册向导(使用 WickedWizard gem)。他们输入的信息创建了Finances模型,该模型属于 User 模型。用户模型 has_one Finance 。

目前,当用户提交第一个财务表单时,页面会刷新而没有任何更新。当我查看我的日志时,我看到:

 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

这是财务注册向导控制器:

  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

我还想考虑用户应该能够再次通过向导并从与之关联的财务模型中查看其数据的用例 - 这就是为什么我认为 if @finance.nil? 节目中需要检查。

编辑

当前正在创建财务模型,但未与用户关联。

4

1 回答 1

0

尝试将您的代码更改为:

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
于 2014-03-31T03:58:33.207 回答