Search code examples
ruby-on-rails-3crudrefinerycms

Refinery CMS - Proper technique to link two models in an engine?


I have used the technique outlined here (http://www.refinerycms.com/guides/multiple-resources-in-an-extension ) to add an engine to an existing refinery CMS (2.0.9) model. This seemed to work well and all of the pages exist and are editable.

However, I am now trying to properly relate two of the models in the engine so that, during entry of new records, the foreign key of one model appears as a drop-down "name" rather than just an empty integer textbox.

For example, my engine name is "gorts" (the robot in "The Day the Earth Stood Still"). Two of the models are device and sensor, and they are properly related as follows: \extension\gorts\models\refinery\gorts\device.rb

module Refinery
  module Gorts
    class Device < Refinery::Core::BaseModel
      attr_accessible :name, :gort_id, :picture_id, :position
      acts_as_indexed :fields => [:name]
      validates :name, :presence => true, :uniqueness => true
      belongs_to :picture, :class_name => '::Refinery::Image'
      belongs_to :gort
      has_many :sensor
    end
  end
end

and sensor.rb:

module Refinery
  module Gorts
    class Sensor < Refinery::Core::BaseModel
      attr_accessible :device_id, :name, :sublocation, :sensortype, :uidval, :position
      acts_as_indexed :fields => [:name, :sublocation, :sensortype, :uidval]
      validates :name, :presence => true, :uniqueness => true
      belongs_to :device
      has_many :reading

    end
  end
end

So, I figured I'll need to modify the sensors controller new action so that it generates an @devices variable... Then it should be trivial to modify the form to turn that into a drop-down. However, everything I try either fails or fails to populate @devices. Here are some things I've tried:

First, I tried adding this to gorts/admin/sensors_controller.rb:

 def new
        # DC Attempting to get @devices defined so we can use for a drop-down
        logger.info("Seeding all devices from admin/sensors_controller new")
        @devices = Device.find(:all)
        super
    end

but it fails with: NoMethodError in Refinery::Gorts::Admin::SensorsController#new super: no superclass method `new' for Refinery::Gorts::Admin::SensorsController:0x007f86ef471d28

If I comment out the "super" line, it seems that I'm breaking the chain somewhere because I get this error on the form: NoMethodError in Refinery/gorts/admin/sensors#new

Showing /Users/cclogicimac/rails_projects/cclogic_app/vendor/extensions/gorts/app/views/refinery/gorts/admin/sensors/_form.html.erb where line #1 raised:

undefined method `model_name' for NilClass:Class
Extracted source (around line #1):

1: <%= form_for [refinery, :gorts_admin, @sensor] do |f| -%>
2:   <%= render '/refinery/admin/error_messages',
3:               :object => @sensor,
4:               :include_object_name => true %>
Trace of template inclusion: vendor/extensions/gorts/app/views/refinery/gorts/admin/sensors/new.html.erb

So, I tried a different approach: Modify /gorts/sensors_controller.rb (instead of gorts/admin...).

So, if I put this code in /gorts/admin/sensors_controller.rb

def new
    # DC Attempting to get @devices defined so we can use for a drop-down
    logger.info("Seeding all devices from sensors_controller new")
    @devices = Device.find(:all)
    super
end

I don't get any errors, but @devices is nil when I get to /views/refinery/gorts/admin/sensors/_form.html.erb. Also I don't think this code is getting hit because my log statement doesn't appear and commenting super has no impact.

What is the proper way to relate two models in a refinery engine so that the input forms show the name of a related model rather than integer keys?

If any Refinery CMS experts could help me out I'd appreciate it and I PROMISE to write up a guide on this topic and send to the Refinery team for inclusion in their guides!

Thanks! Dave


Solution

  • The Simple Way

    In: extensions/gorts/app/views/refinery/gorts/admin/sensors/_form.html.erb

    Just add:

    <div class="field">
      <%= f.label :device -%>
      <%= f.select(:device_id, Refinery::Gorts::Device.all.collect {|d| [d.name, d.id] })%>
    </div>
    

    Instead of the text input you had before.

    It's usually bad practice to query for the Devices do on the view, but this way you don't need to change the default refinery admin controllers which contains crudify and black magic

    The Correct Way

    In: vendor/extensions/gorts/app/controllers/refinery/gorts/admin/sensors_controller.rb

    module Refinery
      module Gorts
        module Admin
          class SensorsController < ::Refinery::AdminController
            before_filter :find_all_devices
    
            crudify :'refinery/gorts/sensors',
                    :title_attribute => 'name', :xhr_paging => true
    
            protected
    
            def find_all_devices
              @devices = Refinery::Gorts::Device.all
            end
    
          end
        end
      end
    end
    

    In: extensions/gorts/app/views/refinery/gorts/admin/sensors/_form.html.erb

    <div class="field">
      <%= f.label :device -%>
      <%= f.select(:device_id, @devices.collect {|d| [d.name, d.id] })%>
    </div>