开发者

how can i have 2 resources share a controller in rails

I've inherited a Rails project, and am fighting with how to deal with a certain situation

Let's say that I have 2 different classes / db tables :

vinyl records
compact discs

Each item in each table has their own item id AND a unique global identifier

example:

vinyl a -- vinyl.1 guid.100
vinyl b -- vinyl.2 guid.200
cd a    -- cd.1 guid.300
cd b    -- cd.2 guid.400

this lets me have nice restful urls like:

/vinyls/1
/cds/1

unfortunately, we don't want that to be the url structure. the structure should be

/recordings/100
开发者_StackOverflow/recordings/300

i'm having a hard time figuring out how to merge all of this together.

my first attempt has me doing the following:

  1. pipe everything through the cds controller
  2. try to find the cd, if i fail - try to find the vinyl
  3. if i find the vinyl, render vinyl/show

that almost works well - the problem is that our partials rely on "resource" quite a bit , and I can't seem to find out how to alter that for the views.

does anyone have a recommendation on how to proceed ? i'm open to anything.


Nothing about a controller says it has to be tied to a model, you can simply have a RecordingsController. That said, trying to deal with different models won't be that straightforward.

If CD and Vinyl have pretty similar columns, you can try Single Table Inheritance. See the Single Table Inheritence section on this page: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

I'm not sure what your problem on views is though; can you explain that a bit more?


The fact that there are two tables here is a problem. I would suggest migrating the database to use Single Table Inheritance where you have a recordings table and have Vinyl and Cd classes than inherit from Recording. Then you can have a recordings_controller.rb and optionally additional controllers for each of the two types. You'll need to add the type column to get the STI magic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜