Ruby on Rails Thursday, October 9, 2014

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)?


On Thu, Oct 9, 2014 at 10:38 AM, Vivek Sampara <ravensnowbird@gmail.com> wrote:
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. 


On Thu, Oct 9, 2014 at 9:07 AM, candor <rcanand@gmail.com> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0bd7fcc4-74a8-46d0-b8da-2dd9272424a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAFKVRj-75B4jz%3DKgv2BUrQP6moqoh3ni-aBvEnyUn5CQWCg-RA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACjeKtFn_OZpx7GKjQ-MTOo1DH6QHzkBrRAe9dz%2B4b_-TKsqzw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment