Array的未定义方法`update'

时间:2014-03-26 00:09:26

标签: ruby-on-rails ruby devise wicked-gem

我正在尝试让Wicked Wizard gem在我的Rails应用程序上工作。一旦用户注册了应用程序(使用Devise),他们就会被重定向到这种形式:

income.html.erb

<%= form_for @finance, url: wizard_path, :method => :put do |f|  %>
  <div class="field">
    What <strong>year</strong> were you born?<br>
    <%= f.number_field :age %>
  </div>
  <div class="field">
    What's your <strong>zip code</strong>?<br>
    <%= f.number_field :zip %>
  </div>
  <%= f.submit %>
<% end %>

我生成了一个名为finances_welcome_controller.rb的控制器来处理wizard_path

class FinancesWelcomeController < ApplicationController
  before_filter :authenticate_user!
  include Wicked::Wizard
  steps :income, :expenses
  def show
    @finance = Finance.find_all_by_user_id current_user[:id] || @finance =     current_user.finances.build(finance_params)
    render_wizard
  end
  def update
    @finance = Finance.find_all_by_user_id current_user[:id]
    @finance.update(params[:finance])
    render_wizard @finance
  end

当我点击submit按钮时,我收到此错误:

NoMethodError in FinancesWelcomeController#update
undefined method `update' for #<Array:0x00000104c6ff48>
Extracted source (around line #14):
    def update
        @finance = Finance.find_all_by_user_id current_user[:id]
        **@finance.update(params[:finance])**
        render_wizard @finance
    end

不确定为什么还没有定义更新方法,因为这与我的资源模型使用的语法相同。 Wicked Wizard gem已在this app上成功实施。

1 个答案:

答案 0 :(得分:1)

find_all_by开始的方法将返回一个Active记录实例数组...而不仅仅是一个。 update仅适用于单个实例。

所以,要么想要使用each来运行数组中的所有实例,要么只想使用find_by而不是find_all_by来获取第一个实例

如果是通过身份查找...我建议将其更改为find_by 这样:

@finance = Finance.find_by_user_id current_user[:id]
@finance.update_attributes(params[:finance])