Search code examples
ruby-on-rails-3formtasticform-forcustomization

How to customize formtastic attribute :as=>:check_boxes


my problem is that I try to customize the formatastic view. But before I go into detail, I'll explain my model.

I have 2 objects with a n:m relation Shop and Category

Shop model looks like that:

has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "shop_id"
has_many :categories, :through => :shop_categories, :source => :categories

Category model looks like that:

has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "category_id"
has_many :shops, :through => :shop_categories, :source => :shops

And of course my m to n table looks like

belongs_to :shops, :class_name => "Shop", :foreign_key => "shop_id"
belongs_to :categories, :class_name => "Category", :foreign_key => "category_id"

validates :shop_id, :presence => true
validates :category_id, :presence => true

This works fine and the following command in my Shop view will list all elements from categories within checkboxes:

<%= f.input :categories, :as => :check_boxes, :id => 'shop_categories' %>

Here is my problem: Within categories I have a name for the category and a picture. Now I want to display the picture next to the selectbox.

I also tryed to use <% f.fields_for :categories do |category| %> but rails wont go through all category elements.

Is there a way to handle all Category elements with automatic checked objects?

If you need more information, i will be glad to give all what you need to understand the problem. Thank you for any hint.


Solution

  • Try overriding custom inputs:

    # app/inputs/collection_check_boxes_input.rb
    class CollectionCheckBoxesInput < SimpleForm::Inputs::CollectionCheckBoxesInput
      # [...]
    end