What is the best way of creating categories in Rails 4? -


i trying different methods of creating categories in rails 4 see works best, far not having joy. not want use gem create thats not answer.

i have set of events want categorize.

can suggest best , convenient way of getting right?

thanks

we've created set of categories our system recently, , relatively simple. use has_and_belongs_to_many:

#app/models/category.rb class category < activerecord::base     has_and_belongs_to_many :events end  #app/models/event.rb class event < activerecord::base     has_and_belongs_to_many :categories end 

schemas:

categories id | name | created_at | updated_at  events  id | name | created_at | updated_at  categories_events category_id | event_id 

this allow call things this:

#app/controllers/events_controller.rb def add_category     @event = event.find(params[:id])     @category = category.find(params[:category_id])      @event.categories << @category #->> 2 activerecord objects end 

heres code update

admin/category.rb

activeadmin.register category      controller        def permitted_params           params.permit category: [:name]        end        def find_resource             scoped_collection.friendly.find(params[:name])         end     end      form |f|       f.inputs "details"         f.input :name       end       f.actions     end  end 

models/category.rb

class category < activerecord::base      has_and_belongs_to_many :events  end 

admin/event.rb

activeadmin.register event      controller        def permitted_params           params.permit event: [:title, :slug, :event_image, :category, :eventdate, :description]        end        def find_resource             scoped_collection.friendly.find(params[:id])         end      def index       @events = current_category.events     end     end      form |f|       f.inputs "details"         f.input :title       end       f.inputs "event date"         f.input :eventdate, :date_select => true, :as => :date_picker, :use_month_names => true       end       f.inputs "category"        f.input :categories, :as => :select, :collection => category.all       end       f.inputs "biography"         f.input :description, :as => :ckeditor, :label => false, :input_html => {  :ckeditor => { :toolbar => 'full', :height => 400 } }       end       f.inputs "image"         f.file_field :event_image       end       f.actions     end  end 

model/event.rb

class event < activerecord::base      has_and_belongs_to_many :categories      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  end 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -