Я новичок в 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) %>
Контроллер продуктов:
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