Hey Everyone, I wanted to know if I could get some insight on something. I am using Rails 2.1.2 and for something that needs beyond basic functionality, I typically generate a scaffold so I don't have to manually create everything and save some time. Well as you all most likely know, along with generating a scaffold, comes predefined actions in the controller that is generated, usually like show, new, create, index, etc... The question I have regarding that is how and where are all those actions tied to? I know Rails 2.* uses the REST model by default, so everything is resourceful. And thats fine and all, but when I want to do something outside of those predefined actions, it throws a fit so to speak. Let me illustrate what I mean by that. So lets say I wanted to create a Rails app for a local brewery that has a UI frontend that would act as the brewery site lets say which also contains a catalogue of their various lagers and ales that they have for sell to take home. So I would create an app called brew lets say - "rails -d mysql brew". So the skeleton is created, but now I need a model, view, and controller of course. I decide this needs a bit extra functionality from the get go so I create a scaffold - "script/generate scaffold beer". I generate a new migration for the table needed to store all the various products that the brewery offers - "script/generate migration add_products". I write in the fields that I want in the db table, and run a rake db:migrate. Everything is great. Now I can add a couple products, and show the details of them. Great. But now, I want to do more than that. Now I want to be able to write in a mashup into the app also so maybe someone could find the place, so I'll integrate google and yahoo maps in with the GeoKit plugin, and I want to play some videos on youtube that related the brewery assuming there were any, so I'd use the YouTube API using something like the YouTube-G plugin or something. So now I install the plugins, and then write some methods into the controller to act as something like a search functionality. But now that say these methods are wrote, and I have a template called search.rhtml in my view that will generate directions to the place using the users remote public ip and geocoding that into coordinates, then pulling up any youtube videos on a sidebar that has to do with the place. So I make sure everything is saved, and I fire up mongrel and test it out. But instead of seeing the generated html form, instead I get an action controller exception saying "Cannot find with ID=search" or something to that affect, and I get lines that describe where it is derived from, but it would be in the controller on the line with the "def show" method, containing something like @beer = Beer.find(params[:id]), in which is is querying for the sql id field, which is the primary key in the table. But because I'm trying to find anything but ID, it fights me tooth and nail. So, I wanted to know if anyone really understand REST, and how everything ties together. I have tried to delete the map.resources :beers (or whatever it may be), but then it has a routing issue. I am but I try to do anything beyond the basic methods laid out in the generated controller and it throws a big fit. Can anyone clarify this for me so I could understand it better?
on 2008-10-28 20:28
on 2008-10-29 23:32
Just a few guidelines so you can learn the rest on your own: Make sure you understand the map.resources method and its capabilities, and especially read the part on adding custom actions (through the :collection, :member and :new options of map.resources): http://apidock.com/rails/ActionController/Resource... REST actions in Rails are probably the least DRY part of the framework, and you'll need to add any custom methods you add to your controller to config/routes.rb file as well. Try reading the first chapter of the book Advanced Rails Recipes (Pragmatic Programmers) if you have access to the book, or just read about REST in Rails in blogs, some have useful information (though some others may be a bit outdated).
on 2008-10-29 23:41
Also, try this: http://guides.rails.info/routing_outside_in.html