为什么Active管理员没有发布与事件相关的选定类别?

时间:2014-03-18 15:32:07

标签: ruby-on-rails ruby ruby-on-rails-4 activeadmin categories

刚刚在activeadmin中创建了与类别模型的关联。这些字段在添加事件时显示在activeadmin区域中,并且它对事件进行了分析,但是类别字段尚未填充选定的字段,这导致它现在不显示在前端并给出问题。

这是我的设置

category.rb

class Category < ActiveRecord::Base

    extend FriendlyId
    friendly_id :name, use: :slugged

    has_many :events
    has_many :profiles

end

管理员/ category.rb

ActiveAdmin.register Category do

  controller do
     def permitted_params
        params.permit category: [:name, :slug]
     end
     def find_resource
      scoped_collection.friendly.find(params[:id])
    end
  end

    form do |f|
      f.inputs "Details" do
        f.input :name
      end
      f.actions
    end

end

event.rb

class Event < ActiveRecord::Base

    has_attached_file :event_image, styles: {
        large: "600x450#",
        medium: "250x250#",
        small: "100x100#"
    }, :default_url => "/images/:style/filler.png"

    validates_attachment_content_type :event_image, :content_type => /\Aimage\/.*\Z/

    validates :title, :slug, :event_image, presence: true

    extend FriendlyId
    friendly_id :title, use: :slugged

    belongs_to :category

end

管理员/ event.rb

ActiveAdmin.register Event do

    controller do
       def permitted_params
          params.permit event: [:title, :slug, :event_image, :event_image_file_name, :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

categories_controller.rb

class CategoriesController < InheritedResources::Base

    def index
        @categories = Category.all
    end

    def show
        @category = Category.friendly.find(params[:id])
    end

end

然后在我看来我得到了这个:

<% @events.each do |event| %>
 <% for category in @categories %>
   <%= category.name %>
 <% end %>
<% end %>

显然它找不到它。

在我的schema.rb中,我有这样的设置:

create_table "categories", force: true do |t|
    t.string   "name"
    t.string   "slug"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "events", force: true do |t|
    t.string   "title"
    t.string   "slug"
    t.date     "eventdate"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "category_id"
    t.string   "event_image_file_name"
    t.string   "event_image_content_type"
    t.integer  "event_image_file_size"
    t.datetime "event_image_updated_at"
  end

add_index "events", ["category_id"], name: "index_events_on_category_id", unique: true
  add_index "events", ["slug"], name: "index_events_on_slug", unique: true

任何人都可以找出为什么不将它添加到现场?

由于

1 个答案:

答案 0 :(得分:1)

您是否在控制器中定义了@events变量?

如果在视图中定义实例变量,则必须在控制器Action中定义它。在索引操作中尝试@events = Event.all