Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:31+00:00 2026-06-16T04:20:31+00:00

I have used the technique outlined here ( http://www.refinerycms.com/guides/multiple-resources-in-an-extension ) to add an engine

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:20:33+00:00Added an answer on June 16, 2026 at 4:20 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this website for testing http://www.webpagetest.org and among some suggestions for optimization
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I've created a hover effect for my website navigation. http://www.preview.brencecoghill.com/ I have got the
I know it can be done in Java, as I have used this technique
If you have used indeed.com before, you may know that for the keywords you
A technique I have used for a long time to do sorting in WPF
Many times I have used an css technique that I learned by myself. When
The technique I have used to hide/unhide a div is as follows: $(# +
What technique have these guys used to get a nice melow dropshadow on the
I have seen two commonly used techniques for adding the directory of the file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.