Search code examples
ruby-on-railscollection-select

Rails use collection_select to assign equipment to site


I have a very simple rails application that collects "equipment" and assigns it to a "site"

    class CreateEquipment < ActiveRecord::Migration[5.0]
  def change
    create_table :equipment do |t|
      t.string :site_name
      t.string :equip_name
      t.string :equip_model
      t.string :equip_make
      t.string :equip_type
      t.belongs_to :site
      t.timestamps
    end
  end
end


class CreateSites < ActiveRecord::Migration[5.0]
  def change
    create_table :sites do |t|
      t.string :site_name

      t.timestamps
    end
  end
end

Two models "Equipment" and "Site"

class Equipment < ApplicationRecord

end

class Site < ApplicationRecord

    has_many :equipment

end

I have set up the equipment belongs_to Site and site has_many Equipment.

In my equipment form I have a collection_select to choose the site to assign the equipment too.

<div class="field">
    <%= f.label :site_name %>
    <%= f.collection_select :site_name, Site.all, :site_name, :site_name, prompt: true %>
  </div>

  <div class="field">
    <%= f.label :equip_name %>
    <%= f.text_field :equip_name %>
  </div>

  <div class="field">
    <%= f.label :equip_model %>
    <%= f.text_field :equip_model %>
  </div>


<div class="field">
    <%= f.label :equip_make %>
    <%= f.text_field :equip_make %>
  </div>
  <div class="field">
    <%= f.label :equip_type %>
    <%= f.text_field :equip_type %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

and on the site show page, I have begun to set up the page to show equipment.

<p>
  <strong>Sitename:</strong>
  <%= @site.site_name %>
</p>

<% @site.equipment.each do |equipment| %>
  <p><%= equipment.equip_name %></p>
<% end %>

but I cannot see any equipment when I open the site show page.

I am not getting any faults or issues on the form or the page, I have allowed all params. I think I may have set up my collection_select incorrectly.

Update: Watching Michael Hartl's Ruby on Rails Tutorial Section 14.1.2 User/relationship associations and feel some enlightenment that anyone with this issue should get privy too.


Solution

  • Well I have learned alot over the time I have been playing with rails. Hopefully when a newbie finds this they will see the very simple error I made.

    I didnt add the association to the site

    class Equipment < ApplicationRecord
    
    end
    
    class Site < ApplicationRecord
    
        has_many :equipment
    
    end
    

    should be

    class Equipment < ApplicationRecord
     belongs_to :site
    end
    
    class Site < ApplicationRecord
    
        has_many :equipment
    
    end
    

    and the table should change from

    class CreateEquipment < ActiveRecord::Migration[5.0]
      def change
        create_table :equipment do |t|
          t.string :site_name
          t.string :equip_name
          t.string :equip_model
          t.string :equip_make
          t.string :equip_type
          t.belongs_to :site
          t.timestamps
        end
      end
    end
    
    class CreateEquipment < ActiveRecord::Migration[5.0]
      def change
        create_table :equipment do |t|
          t.string :equip_name
          t.string :equip_model
          t.string :equip_make
          t.string :equip_type
          t.references :site
          t.timestamps
        end
      end
    end
    

    I hope that helps someone :)