0

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
4

1 回答 1

1

当您使用#find 并且找不到记录时,ActiveRecord 会引发 ActiveRecord::RecordNotFound 并显示一条消息,例如“找不到具有 id='somevalue' 的公司”。

我假设您的 id 列是整数类型,并且您传递了一个字符串。

在你的#show 方法中 params[:id] == 'info'。

检查你的 link_to、redirect_to 和路由。

在某些时候,您会生成此 URL http://localhost:3000/company_steps/info(可能在视图中)。

您对其执行 GET 请求,该请求与 GET "/company_steps/:id" company_steps#show 匹配。

#show 方法在控制器 CompanyStepsController 中调用,参数为 params[:id] == 'info'。

正如我们之前看到的,您会收到 ActiveRecord::RecordNotFound 异常,因为 ActiveRecord 找不到 ID 为“信息”的记录。

该错误在您的控制器中引发,但问题可能出在您的视图或重定向中。您需要一个 id 并传递一个字符串。

编辑:如评论中所述

Ok params[:id] == 'info' 是由 wicked 生成的。他们使用 id 来控制步骤的流程。您需要使用嵌套路由让 rails 生成类似 params[:company_id] 的内容。

资源:公司做资源:步骤,控制器:'公司/步骤'结束

所以 rake 路线应该给你:/companies/:company_id/steps/:id

在控制器中 params[:company_id] == 42 params[:id] == 'info'

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

于 2016-01-13T22:36:15.897 回答