Question

I have an ActiveAdmin project that has a has_and_belongs_to_many association. The index page and form represent this as I like. When I save or edit a record the associations is saved properly. However, when you save and are redirected to the show page the field for the association says empty. How can I customize the show page to show all records updated?

here is my AA model:

ActiveAdmin.register Products do


  # MENU
  menu :parent => "Items"


  # FILTER
  filter :name, :as => :select


  # CONTROLLER
  controller do

    def new
      @data = ItemsCategory.all
      $tree = build_category_tree(@data)

      @items_design = ItemsDesign.new
      @items_design_show_id = nil
      $hidden = "<input type=\"hidden\" name=\"items_design[items_category_ids][]\" value=\"#{params[:category_id]}\">".html_safe
    end

    def autocomplete_tags
      @tags = ActsAsTaggableOn::Tag.
          where("name LIKE ?", "#{params[:q]}%").
          order(:name)
      respond_to do |format|
        format.json { render json: @tags, :only => [:id, :name] }
      end
    end

    def build_category_tree(data, indent = 2)
      d = data.each_with_object({}) { |h, g| g[h[:id]] = h }
      options = ""

      d.each { |_, h| h[:ancestor_ids] =
          (h[:top_level_category_id] ? d[h[:parent_id]][:ancestor_ids] : [])+[h[:id]] }
      .values
      .sort_by { |h| h[:ancestor_ids] }
      .each do |h|
        if h[:id] == @selected
          options << "<option selected value=\"#{h[:id]}\">" + "&nbsp;&nbsp;" *((h[:ancestor_ids].size-1)*indent) + "#{h[:name]}</option>"
        else
          options << "<option value=\"#{h[:id]}\">" + "&nbsp;&nbsp;" *((h[:ancestor_ids].size-1)*indent) + "#{h[:name]}</option>"
        end

      end

      tree = "<select class=\"chosen\" name=\"items_design[items_category_ids][]\">" << options << "</select>"

      tree.html_safe
    end




    def edit
      @items_design_show_id = nil

      @items_design_id = request.fullpath.split('/').reject! { |c| c.empty? }
      @items_design = ItemsDesign.find_by_id(@items_design_id)

      @selected = @items_design.items_categories[0].id

      $tree = build_category_tree(@data)

    end
  end


  # INDEX
  index do
    column :id
    column :name
    column "Description" do |desc|
      truncate(desc.description, omision: "...", length: 100)
    end
    column "Image" do |items_design|
      image_tag "#{items_design.image_name}", class: "item-image"
    end
    column :style
    column :brand
    column :color
    column "Items Categories" do |category|
      (category.items_categories.map { |p| p.name }).join(', ').html_safe
    end
    column "Colors" do |c|
      (c.colors.map { |p| p.name }).join(', ').html_safe
    end
    column :make
    column :like
    column :price
    column :product_url
    column :tag_list
    default_actions
  end


  form do |f|
      f.inputs "Details" do
        f.inputs do
          $tree
        end
      f.input :item
      f.input :name
      f.input :company
      f.input :description
      f.input :style
      f.input :brand
      f.input :color

      # select2 is used for multi-select + on-the-fly tag creation per the article below
      # http://hoff2.com/2013/11/09/acts_as_taggable_on_active_admin_select2.html
      f.input :tag_list,
              label: "Tags",
              input_html: {
                  data: {
                      placeholder: "Enter tags",
                      saved: f.object.tags.map { |t| {id: t.name, name: t.name} }.to_json,
                      url: autocomplete_tags_path
                  },
                  class: 'tagselect'
              }
      f.input :colors, :as => :select
      f.input :make
      f.input :like
      f.input :price
      f.input :product_url
    end
    f.actions
  end

end
Était-ce utile?

La solution

This did it for me:

show do
    attributes_table do
      row :id
      row 'Category' do |n|
        n.categories.map(&:name).join("<br />").html_safe
      end
      row :name
      row :description
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top