form_for defaults to ‘new’ if no :url is specified. Why doesn’t it default to ‘create’ instead, since that is the most common scenario?
It depends on the type of object you pass as a parameter to form_for.
eg:
@car = Car.new
@car1 = Car.first
form_for(@car) generates a url ( /cars ) method “post” #create action
form_for(@car1) generates a url ( /cars/:id/ ) method “put” # update action. this happens when the object has been saved in the database already
form_for doesn’t generate a url pointing to :new action unless specified.
Thanks, Vivek.
If I use form_for :car
It always submits to current url/action by default.
If you use the more RESTful approach (as described by Vivek) it posts to appropriate actions.
My question is, in the :car case, wouldn’t it be DRY-er to directly pick #create? (i.e, user doesn’t have to explicitly do @car = Car.new when dealing with a new form)?