views:

286

answers:

1

Hi All,

I'm currently managing multiple models from a single SITE MANAGER page. I have the following relationships:

Sites -> Buildings -> Meters -> Values

Beside each "Sites" item I have "Edit | Delete | Add Building" Beside each "Building" item I have "Edit | Delete | Add Meter" Beside each "Meter" item I have "Edit | Delete | Add Value" Beside each "Value" item I have "Edit | Delete"

At this point I have one frankensteined controller called "SiteManagerController" which manages this page. I simply have a method (and corresponding route in the routes file) like so:

add_site
add_building_to_site
add_meter_to_building

delete_site
delete_building
delete_meter

What I'm wondering, however, is whether or not there is a quality mechanism by which to use the existing item controllers CRUD methods while being able to render the appropriate RJS file for the "SiteManager" page and controller?

It would be nice if I could somehow route to the original controller (so as to not have to rewrite all the methods manually) while also having rails redirect control (not just visually, but contextually) back to the "SiteManager" controller after creating or deleting. Keep in mind that I'm not dealing with forms, but simply an INDEX page and multiple "link_to_remote"s

I'm quite possibly asking the wrong question, so do consider that...in any event, I'm open to suggestion.

Best.

A: 

You can absolutely use the exiting item controllers CRUD methods. You can point link_to_remote at any url and it will insert the html you instruct it to. As long as you keep the default routes in routes.rb, everything should work fine. This will keep the user on the SiteManager page, but he will be interacting with the RESTful routes behind the scenes.

link_to_remote "Edit", :update => "site_#{site.id}",
     :url => site_url(site), :method => :put
link_to_remote "Add Building", :update => "new_building", :url => buildings_url,
     :method => :post

SitesController < ApplicationController
  def update
    @site = Site.find(params[:site_id])
    @site.update_attirbutes!(params[:site_id])
    render :partial => @site
  end
end

BuildingsController < ApplicationController
  def create
    @building = Building.create(params[:building])
    render :partial => @building
  end
end
erik
Understood, but how do I then "render :update" and affect the "SiteManager" index page? Do I place that in each item's controller? How will it know to affect a partial or grab rjs from the "SiteManager" context when it's done?
humble_coder