2

I'm trying to get the Wicked Wizard gem working on my Rails app. Once a user registers for the app (using Devise), they're redirected to this form:

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 %>

I generated a controller called finances_welcome_controller.rb that handles 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

When I click the submit button, I'm getting this error:

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

Not sure why the update method hasn't been defined since this is the same syntax that my resource's model is using. The Wicked Wizard gem was successfully implemented on this app.

sawa
  • 165,429
  • 45
  • 277
  • 381
beaconhill
  • 441
  • 6
  • 28

1 Answers1

1

a method starting find_all_by will return an array of Active record instances... not just a single one. update only works on a single instance.

So, either you want to run through all the instances in the array... using an each - or you want to just fetch the first one using find_by instead of find_all_by

In the case of finding by an id... I'd recommend changing it to find_by so:

@finance = Finance.find_by_user_id current_user[:id]
@finance.update_attributes(params[:finance])
Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • Thanks for the response - this seems to have fixed the form submission error, but unfortunately the data is not actually saving. I have my users associated with Finances, and when I hit my _form for editing Finances, the data isn't updated with what was submitted from the wizard. – beaconhill Mar 26 '14 at 00:32
  • @beaconhill - try `update_attributes`. Or ideally `@finance.assign_attributes(params[:finance]); finance.save!` ` – BroiSatse Mar 26 '14 at 00:35
  • It's saving when I go back to the Wizard page (/finance_welcome/income which has part of the form) but not when I'm on the actual finance edit page (/finance/6/edit) – beaconhill Mar 26 '14 at 00:39
  • You will need to provide further code/information for us to look at then. Is your form calling the update action in your controller>? what params are coming into `params[:finance]` ? is it what you expect? etc – Taryn East Mar 27 '14 at 03:22