0

Hello guys I am new to Ruby . I am try to create form with form builder What is difference between

<%=form_for @post %>

and

<%=form_for :post %>

2 Answers2

2

form_for creates an instance of the form builder object:

A FormBuilder object is associated with a particular model object and allows you to generate fields associated with the model object. The FormBuilder object is yielded when using form_for or fields_for


@post is an instance variable. :post isn't

This means that when you load a <%= form_for @post %>, you're populating the form builder object with data from the instance (allowing Rails to maintain the data when you have errors etc)

If you're unsure about @instance_variables in Ruby, you might want to check out the Ruby On Rails beginners' guide

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • It can be a lot of things (I can't give a concise answer unfortunately). Here's a really good reference for you: http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby – Richard Peck Feb 12 '14 at 09:22
  • 1
    I only know `:symbols` are used mainly to determine arguments for Rails methods (I.E `<%= form_for @post, method: :post %>` – Richard Peck Feb 12 '14 at 09:22
  • 1
    `:post` is just namespace for a future object and also html
    namespace. `@post` is existing instance variables with Post properties.
    – itsnikolay Feb 12 '14 at 09:25
0

if you are use symbol :post like

<%=form_for :post %>

then it create

<form action="/posts" method="post">

if you are use instance @post like

<%=form_for @post %>

then it create

 <form action="/posts/create" class="class_name" id="id_name" method="post">
Dheer
  • 773
  • 1
  • 6
  • 16
  • I think, When the model is represented by a string or symbol, if the :url option is not specified, by default the form will be sent back to the current url – Thaha kp Feb 12 '14 at 10:52