我正在尝试使用HABTM方法向rails中的事件添加类别。由于某种原因,它根本不起作用。还有什么奇怪我在活动管理员中的id字段填充了我的事件的标题而不是像这样的id整数:/admin/events/event-test
我有这个设置:
模型/ event.rb
class Event < ActiveRecord::Base
has_and_belongs_to_many :categories
self.primary_key = :id
has_attached_file :event_image, styles: {
large: "600x450#",
medium: "250x250#",
small: "100x100#"
}, :default_url => "/images/:style/filler.png"
validates :title, :slug, :event_image, presence: true
extend FriendlyId
friendly_id :title, use: :slugged
end
管理员/ event.rb
ActiveAdmin.register Event do
controller do
def permitted_params
params.permit event: [:title, :slug, :event_image, :category, :eventdate, :description]
end
def find_resource
scoped_collection.friendly.find(params[:id])
end
end
form do |f|
f.inputs "Details" do
f.input :title
end
f.inputs "Event Date" do
f.input :eventdate, :date_select => true, :as => :date_picker, :use_month_names => true
end
f.inputs "Category" do
f.input :categories, :as => :check_boxes, :collection => Category.all
end
f.inputs "Biography" do
f.input :description, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } }
end
f.inputs "Image" do
f.file_field :event_image
end
f.actions
end
end
模型/ category.rb
class Category < ActiveRecord::Base
has_and_belongs_to_many :events
self.primary_key = :id
end
管理员/ category.rb
ActiveAdmin.register Category do
controller do
def permitted_params
params.permit category: [:id]
end
def find_resource
scoped_collection.friendly.find(params[:name])
end
end
form do |f|
f.inputs "Details" do
f.input :name
end
f.actions
end
end
schema.rb
create_table "categories", id: false, force: true do |t|
t.integer "id"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "categories_events", force: true do |t|
t.integer "category_id"
t.integer "event_id"
end
events_controller.rb
class EventsController < InheritedResources::Base
def index
@events = Event.all
end
def show
@event = Event.friendly.find(params[:id])
end
end
categories_controller.rb
class CategoriesController < InheritedResources::Base
def index
@categories = Category.all
end
def show
@category = Category.find(params[:id])
end
end
当我进入我的视图并尝试访问所选类别时,我得到[]。
有谁知道这里的问题是什么?