我有一个分类模型。创建类别时,必须选择类别将具有的大小。 Shirt
类别将具有XS,S,M,L,XL,XXL。
我使用enum sizes:
创建了各种尺寸的衣服,这是我的分类模型。
出于某种原因,当我运行Category.last时,它会显示sizes.nill。我如何使这项工作?
2.2.1 :003 > Category.last
Category Load (0.4ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" DESC LIMIT 1
=> #<Category id: 25, name: "test", ancestry: "20", created_at: "2015-09-03 11:27:40", updated_at: "2015-09-03 11:27:40", sizes: nil>
类别控制器
class CategoriesController < ApplicationController
before_action :set_category, only: [:show]
before_action :admin_user, only: [:destroy, :index, :edit]
def index
@categories = Category.all
end
def show
@tags = Item.where(category_id: @category.id).tag_counts_on(:tags)
if params[:tag]
@items = Item.tagged_with(params[:tag])
else
@items = Item.where(category_id: @category.id).order("created_at DESC")
end
end
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
flash[:success] = "You have created a new category"
else
flash[:danger] = "Your category didn't save"
render "new"
end
end
def update
@Cateogry = Category.find(params[:id])
if @Cateogry.update(category_params)
redirect_to @Cateogry
flash[:success] = 'Category was successfully updated.'
else
render "edit"
end
end
def destroy
Category.find(params[:id]).destroy
flash[:success] = "Category deleted"
redirect_to categories_path
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :sizes, :parent_id)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.try(:admin?)
end
end
类别模型
class Category < ActiveRecord::Base
has_ancestry
has_many :items
validates :name, presence: true, length: { maximum: 20 }
enum sizes: [:XSmall, :Small, :Medium, :Large, :XLarge, :XXLarge,
:'1', :'2', :'3', :'4', :'5', :'6', :'7', :'8', :'9', :'10',
:'11', :'12', :'13', :'14', :'15', :'16', :'17', :'18', :'19', :'20',
:'21', :'22', :'23', :'24', :'25', :'26', :'27', :'28', :'29', :'30',
:'31', :'32', :'33', :'34', :'35', :'36', :'37', :'38', :'39', :'40',
]
end
类别创建视图
<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(@category) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<div class ="form-inline">
<%= f.input :sizes, collection: Category.sizes.keys, as: :check_boxes, input_html: { multiple: true }, label:"Select Size" %>
</div>
<%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
我的类别中的尺寸迁移。
class AddSizesToCategories < ActiveRecord::Migration
def change
add_column :categories, :sizes, :integer, array: true
end
end
模式
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "ancestry"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "sizes"
end
答案 0 :(得分:0)
应该对enum列名称进行单一化,并使用枚举列的复数名称获取所有可用值的列表。
在您的示例中,该列应该被称为size
,并且可以使用Category.sizes
访问所有尺寸的列表,然后
答案 1 :(得分:0)
我认为存在一种误解:
枚举不一组。枚举可以包含一组给定值的一个。
因此,如果您想要处理每个类别的多个尺寸,您需要一个关联(has_many:尺寸,使用不同的模型Size
)或使用serialisation。
sizes
为空的原因:
表单返回一个名为sizes
的数组和所选的选项
您的category_params
仅允许标量:sizes
,因此强参数会拒绝值数组。但是如果你允许sizes: []
,那么分配给整数就会失败。