2014-03-26 151 views
1

我試圖讓我的Rails應用程序中的邪惡嚮導gem工作。一旦該應用(使用設計)的用戶寄存器,他們重定向到這種形式:陣列的未定義方法`更新'

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 

不確定爲什麼沒有定義更新方法,因爲這與我的資源模型使用的語法相同。邪惡精靈寶石成功實施this app

回答

1

啓動find_all_by的方法將返回一個活動記錄實例的數組......不僅僅是一個單獨的活動記錄實例。 update只適用於單個實例。

所以,要麼你想使用each通過陣列中的所有實例運行... - 或者你只想獲取使用find_by代替find_all_by

在通過發現的情況下,第一個ID ...我建議將其更改爲find_by 這樣:

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

感謝您的答覆 - 這似乎已經解決了這一表單提交錯誤,但不幸的是數據不實際保存。我有我的用戶與財務相關聯,並且當我用我的_form編輯財務時,數據不會更新從嚮導提交的內容。 – beaconhill

+0

@beaconhill - 嘗試'update_attributes'。或理想情況下'@ finance.assign_attributes(params [:finance]); ' – BroiSatse

+0

當我回到嚮導頁面(/ finance_welcome /收入具有部分表格)時,它會保存,但當我在實際的金融編輯頁面時(/ finance/6/edit) – beaconhill

相關問題