Mike Glaz
Mike Glaz

Reputation: 5382

Understanding function form_for() in Rails

I'm currently reading Beginning Rails 3. I'm coming from PHP and trying to learn Ruby and Rails. I'm looking at a _form partial and I have a few questions. Specifically about the line:

    <%= form_for(@article) do |f| %>

What is the purpose of having the @article object in there as well as what is the function of variable f?

thanks, mike

Upvotes: 1

Views: 247

Answers (2)

Dave Newton
Dave Newton

Reputation: 160170

The @article is what the form is for (in this case).

The f is for creating individual form elements; it's a builder object yielded by form_for's block.

Upvotes: 0

Nevir
Nevir

Reputation: 8101

form_for accepts a model so that it can do a few things for you under the covers:

  • It will read any current values off of that model and populate them in the fields you specify
  • It will generate the proper URL for that resource (assuming you're following conventions, otherwise you still have to specify it)
  • It can display any validation errors on the model if you're displaying after a POST.

If you just want the tag helpers, there's also form_tag and friends

Upvotes: 4

Related Questions