views:

104

answers:

2

I seriously cant understand why this is so hard... I have some experience with other mvc frameworks but always heard rails was the easiest to code in.... right now I cant even get to my controller methods if i want to.

I used scaffold to creat 'student' which automatically created for me the controller, model and views for basic CRUD.. but now I just want to add a method "helloworld" to my controller and when i go to

http://localhost:3000/students/helloworld

I get a

Couldn't find Student with ID=helloworld

error.

what am I missing?.. I know its got to do with routes and the REST thing but I still cant figure out then how else am I supposed to use my own methods... do I have to edit my routes.rb file everytime I create a new method?.. please help

+4  A: 

Routes for models in Rails are divided into 2 groups. Ones that act on a single objects (think edit, update, delete) and ones that don't have a single object to act on (new, index). If you want to create your own method that doesn't take an object ID you need to add a route config for that method in your routes file. The methods are either member or collection methods. Member methods URLs look like /model/id/method_name. Collection methods look like what you want (/model/method_name). Here is an example for your students model (routes.rb)

map.resources :students, :member => {:some_member_function_example => :get },
                           :collection => { :helloworld => :get }

Note: You can just remove the :member => ... from the config and only have collection if you have no member methods to define.

ScottD
OK, so I have to be editing my routes.rb throughout the development... I hate that... I was used to just havning to edit routes if you wanted something fancy (in codeigniter)... the default was always controller/method/param1/param2/param3/param4, etc.I have one more question.. I want to change the names of the methods the scaffold created.. cause my app is in spanish.so for 'new' I want 'nueva', for 'create' I want 'crear'.....
NachoF
I would look at the internationalization rails wiki for articles that can help you out. http://rails-i18n.org/wiki
ScottD
A: 

Link /students/foo will not call the foo method of the students_controller. That's because REST mappings in Rails includes /:controller/:id route for GET. And your link matches this pattern.

In order to override that path (for methods with no parameters, like yours) use the following snippet:

map.resources :students, :collection => {:method_name => :get}
Henryk Konsek