Dmitry
Dmitry

Reputation: 7553

form_for :subject

I've just started learning ruby, so this question is simple.

I created @subject in controller.
Why is :subject used in form_for (instead of @subject)?

<%= form_for(:subject, :url => {:action => 'create'}) do |f| %>

Upvotes: 0

Views: 193

Answers (2)

aubreyrhodes
aubreyrhodes

Reputation: 787

According to the documentation, you could use either (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for).

Passing in the symbol :subject will create a generic form for a resource named 'subject'. Passing in the instance variable @subject will create a form for the specific instance of the Subject class and figure out the correct url for you (assuming you're following the standard rails conventions). The documentation mentions that using the latter method with the instance variable is the preferred method.

Upvotes: 1

YaBoyQuy
YaBoyQuy

Reputation: 793

It is similar to the assigns in rspec rails testing (you can look it up, assigns(:subject) will seek out for a @subject in your controller). Correct me if I am wrong but I think it is because the symbol :subject will try to seek out for an instance variable in your controller that correspond with @subject. So to prove it, rename your @subject in your controller to something else, like @subjectz and I think it won't work anymroe

Upvotes: 1

Related Questions