Do you have defined the @events
variable in the controller?
If you define an instance variable in the view, it has to be defined in the controller Action. Try @events = Event.all
in your index action.
문제
Just created an association with a model for category in activeadmin. The fields show up in the activeadmin area when adding an event and its aves the event but the category field has not been populated with the selected one and this is causing it to now not show on the frontend and give issues.
here is my setup
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_many :events
has_many :profiles
end
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
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
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
class CategoriesController < InheritedResources::Base
def index
@categories = Category.all
end
def show
@category = Category.friendly.find(params[:id])
end
end
Then in my view I have got this:
<% @events.each do |event| %>
<% for category in @categories %>
<%= category.name %>
<% end %>
<% end %>
Obviously it cannot find it.
In my schema.rb I have this setup:
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
Can anyone figure out why it would not add it into the field?
Thanks
해결책
Do you have defined the @events
variable in the controller?
If you define an instance variable in the view, it has to be defined in the controller Action. Try @events = Event.all
in your index action.