我正在创建一个在线零售店。那个属于一个类别的物品。当我尝试提交带有选定类别的项目时,它将不会保存
我试了好几个小时来解决但却无法理解。有谁知道问题是什么?
错误是
ActiveRecord::AssociationTypeMismatch in ItemsController#create
Category(#70298375791060) expected, got String(#70298372605800)
def create
@item = current_user.items.build(item_params)
if @item.save
redirect_to @item
flash[:success] = "You have created a new item"
项目表格
<h1>Create New item</h1>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for @item, html: { multipart: true } do |f| %>
<%= f.input :image%>
<%= f.collection_select :category, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
<%= f.input :title%>
<%= f.input :price %>
<%= f.input :description %>
<%= f.button :submit, "Create new item", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
项目控制器
class ItemsController < ApplicationController
before_action :correct_user_edit, only: [:edit, :update, :destroy]
def index
@item = @user.items.paginate(page: params[:page])
end
def new
@item = Item.new
end
def home
@items = Item.paginate(page: params[:page])
end
def edit
@item = Item.find(params[:id])
@user = User.find(params[:id])
end
def show
@item = Item.find(params[:id])
end
def update
@item = Item.find(params[:id])
if @item.update(item_params)
redirect_to @item
flash[:success] = 'Item was successfully updated.'
else
render "edit"
end
end
def create
@item = current_user.items.build(item_params)
if @item.save
redirect_to @item
flash[:success] = "You have created a new item"
else
flash[:danger] = "Your item didn't save"
render "new"
end
end
def destroy
Item.find(params[:id]).destroy
flash[:success] = "Item deleted"
redirect_to users_url
end
private
def item_params
params.require(:item).permit(:title, :category, :price, :description, :image)
end
#Check to see if user can edit item.
def correct_user_edit
if @item = current_user.items.find_by(id: params[:id])
else
flash[:danger] = "You can't edit that item"
redirect_to root_url if @item.nil?
end
end
end
项目模型
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :category
validates :category, presence: true
validates :title, presence: true, length: { maximum: 30 }
validates :price, presence: true
validates :description, presence: true, length: { maximum: 2000 }
validates :user_id, presence: true
has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
类别模型
class Category < ActiveRecord::Base
has_ancestry
has_many :items
end
答案 0 :(得分:2)
ItemsController #create中的ActiveRecord :: AssociationTypeMismatch 类别(#70298375791060)预期,得到字符串(#70298372605800)
这一行
<%= f.collection_select :category, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
应该是
<%= f.collection_select :category_id, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
并且还改变了item_params
def item_params
params.require(:item).permit(:title, :category_id, :price, :description, :image)
end
未定义的方法`category_id&#39;对于项目:0x007fdf43bebed0
category_id
表中应该有items
列。运行以下命令,该命令将创建一个迁移文件,以便将category_id
添加到items
。
rails g migration add_category_id_to_items category_id:integer
和run rake db:migrate
我建议你在进一步阅读之前阅读这些Guides。
答案 1 :(得分:0)
而不是<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="gsie.beezlink.OpportunityActivity">
<Button
android:layout_width= "match_parent"
android:layout_height="wrap_content"
android:text="@string/posterbtn"
android:background="#008000"
android:id="@+id/posterbtn"
android:gravity="center"
android:layout_alignParentBottom="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cinteretxt"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/cinteretxt"
android:layout_alignParentTop="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cinteretspin"
android:layout_below="@+id/cinteretxt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/optitretxt"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/optitretxt"
android:layout_below="@+id/cinteretspin"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/optitreedt"
android:layout_below="@+id/optitretxt"
android:hint="@string/hintoptitre" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/optxt"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/optxt"
android:layout_below="@+id/optitreedt"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/opedt"
android:hint="@string/hintpost"
android:layout_below="@+id/optxt" />
</RelativeLayout>
,您应该传递category
param。