ruby-on-rails - 为什么会出现关联类型不匹配?

标签 ruby-on-rails associations

我是 RoR 的新手,遇到了一个让我发疯的问题,我无法弄清楚!

基本上,我创建了一个产品表单并添加了一个“供应商”选择字段,它可以找到我的所有供应商,但是现在当我尝试更新当前产品或创建一个新产品时,出现以下错误:

ActiveRecord::AssociationTypeMismatch in ProductsController#create
Supplier(#2178099880) expected, got String(#2148287480)

我明白错误告诉我什么,但我不明白为什么。我也以同样的方式为产品类别创建了一个关联,效果很好!

此外,如果我添加验证来验证供应商的存在,即使选择了供应商,我也会收到“供应商不能为空”错误。

真的很感激这方面的一些帮助!

在我的 _form.html 我有:
<%= f.label :supplier %>
<%= f.select(:supplier, Supplier.find(:all).collect {|c| [ c.name, c.id ]}, :include_blank => true) %>

产品 Controller :
class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(products_url) }
      format.xml  { head :ok }
    end
  end
end

产品型号:
class Product < ActiveRecord::Base

  belongs_to :supplier
  belongs_to :categories
  default_scope :order => 'product_number'

  validates_presence_of :product_name, :product_number, :category, :opening_order, :initial_cover,
  :country_of_origin, :supplier

  validates_uniqueness_of :product_number

  # Paperclip

  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

最佳答案

不要为 supplier 创建表单字段,而是尝试为 supplier_id 创建它们:

<%= f.label :supplier_id %>
<%= f.select(:supplier_id, Supplier.find(:all).collect {|c| [ c.name, c.id ]}, :include_blank => true) %>

关于ruby-on-rails - 为什么会出现关联类型不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583444/

相关文章:

ruby-on-rails - rails : Show all associated records on a has_many through association

postgresql 困难(对我来说)查询

ruby-on-rails - 如何实现版本化的 ActiveRecord 关联?

ruby-on-rails - 使用 rails 的 heroku pg 错误,说 "text"不允许使用类型修饰符

ruby-on-rails - 安装mysql(2.9.1)时出错,Bundler无法继续

ruby-on-rails - 如何使用 Rails 进行扩展

ruby-on-rails-4 - 尽管在我的模型中包含 ":autosave => true",但 Rails 并未保存我的关联

ruby-on-rails - 如何在 Rails 3 中使用 OR 条件合并两个事件记录关系并返回结果也是事件关系而不是数组?

ruby-on-rails - 缓存特定的部分 rails 3.0.x

ruby-on-rails-4 - 通过 has_many 关联的 rails 范围