views:

243

answers:

3

Let's say I have a SessionsController, which controls user login and logout, but the only actions I really need are new (for displaying login form), create (for authentication and login) and destroy for logging out the user.

Is there any problem if I just have these three actions in my controller, or do I have to implement them all to make it correctly RESTful?

And second little question. Some people say that scaffolding is bad, and that one should write code by hand, but I find it pretty useful and time saving.

Is it OK to use scaffolding, or is it evil that should be avoided and why?

+4  A: 

There is nothing wrong with implementing only those actions which you need. In Rails 2.3 you can be even more explicit in your routes as well.

map.resources :foo, :only => [:create, :destroy, :new]

jdl
+8  A: 

It is definitely okay to only create the RESTful actions which you want to support for that resource. You do not have to define all 7 actions. In fact, the majority of my controllers do not use all 7 actions.

Is it OK to use scaffolding, or is it evil that should be avoided and why?

The built-in Rails scaffolding is mainly designed to help get off the ground when beginning. I personally don't use it for everyday development for a few reasons.

  • it generates a CSS and layout file when I want to use the existing application one
  • it generates all controller actions (like I said I usually don't want all of them)
  • it creates an XML format for every action which I almost never want
  • it does not put the form in a partial when I need both "edit" and "new" actions
  • I sometimes use a different testing library (such as Shoulda or RSpec)

However, I am a fan of scaffolding for speeding up development. This is why I created the nifty_scaffold generator which I use almost all the time. It does not have the problems mentioned above.

ryanb
+1 - Great answer!
Matthew Murdoch
+1  A: 

Remember REST only really has 4 verbs GET, POST, UPDATE PUT and DELETE. The edit and new actions in Rails are workarounds of the GET verb to display the form for editing the resource. The index action is a GET on a different resource, its just bundled with the others because in an application development context it makes more sense to have it in that same file.

Rare Pleasures
REST does not define any actions. I'm presuming you are referring to the four main HTTP verbs GET, PUT, POST and DELETE. HTTP does have other verbs though.
Darrel Miller