ruby-on-railsformsruby-on-rails-4wicked-gem

:id => "info" error rails wicked forms when retrieving params


I'm new to wicked form and I was following the railcast episode on wicked forms but I keep receiving this error "Couldn't find Company with 'id'=info". So I know that the problem is clearly in my controllers somewhere. I know it's something super simple that I'm just racking my brain on so I know you guys will be a giant help. Here is the code, any and all help appreciated!

Code for companies Controller:

def create
@company = Company.new(company_params)

respond_to do |format|
  if @company.save
    @object = @company.id
    format.html { redirect_to(company_steps_path(@company)) }
    format.json { render :show, status: :created, location: @company }
  else
    format.html { render :new }
    format.json { render json: @company.errors, status: :unprocessable_entity }
  end
end
  end

Code for company_steps Controller:

class CompanyStepsController < ApplicationController


include Wicked::Wizard

  steps :info, :address, :quote

  def show
    @company = Company.find(params[:id])
    render_wizard
  end
  def update
    @company = Company.where(id: params[:id])
    @company.attributes = params[:company]
    render_wizard @company
  end
end

Solution

  • When you use #find and the record is not found ActiveRecord raise a ActiveRecord::RecordNotFound with a message like "Couldn't find Company with id='somevalue'".

    I assume your id column is of type integer and you pass a string.

    In your #show method params[:id] == 'info'.

    Check your link_to, redirect_to and routes.

    At some point you generate this url http://localhost:3000/company_steps/info (probably in a view).

    You do a GET request on it, which match GET "/company_steps/:id" company_steps#show.

    The method #show is call in the controller CompanyStepsController with params[:id] == 'info'.

    As we see previously you get a ActiveRecord::RecordNotFound exception because ActiveRecord can't find the record with a id 'info'.

    The error is raise in your controller, but the problem is probably in your views or in a redirect. You need a id and you pass a string.

    EDIT: as discussed in comments

    Ok params[:id] == 'info' is generated by wicked. They use id to control the flow of steps. You need to use nested routes to have rails generate something like params[:company_id].

    resources :companies do resources :steps, controller: 'companies/steps' end

    So rake routes should give you: /companies/:company_id/steps/:id

    in the controller params[:company_id] == 42 params[:id] == 'info'

    https://github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step