Question

I am creating a category select dropdown to add categories to an event.

I can add them and they are showing in the form for the edit area, I can see the categories added in the backend.

When I try to show them in the views it gives me this strange error inside the layout:

<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Category:0x00000102542f10>

In my setup I have got this:

views/events/index.html.erb

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

models/category.rb

class Category < ActiveRecord::Base

    belongs_to :event
    belongs_to :profile
    belongs_to :classified
    belongs_to :page

    extend FriendlyId
    friendly_id :name, use: :slugged

end

admin/category.rb

ActiveAdmin.register Category do

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

    form do |f|
      f.inputs "Name" do
        f.input :name
      end
      f.inputs "Description" do
        f.input :description, :as => :ckeditor, :label => false, :input_html => {  :ckeditor => { :toolbar => 'Full', :height => 400 } }
      end
      f.actions
    end

end

In my categories table there is an event_id column in there so it can fnd the associated event and it links to both the events table and the categories table.

Any insight into this would be great

Thanks

Was it helpful?

Solution

Update views/events/index.html.erb as below:

<% if event.categories.count > 0 %> 
      <% event.categories.each do |category| %> ## Use <% %> to evaluate ruby code
        <%= category.name %>               ## Use <%= %> To evaluate and show the results
      <% end %>
<% end %>         

In your case, <%= %> will return event.categories value which is an instance of <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Category which is what you see on the rendered page.

UPDATE

OP also had foreign_key concerns. Add foreign_keys for event, profile, classified and page in categories table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top