Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instance variable vs. symbol in ruby on rails (form_for)

i'm new to ruby on rails and am working with version 2.3 on mac osx. i am trying to create the same functionality a scaffold creates, but on my own. i created a "post" controller, view, and model. in post controller, i have the following:

class PostController < ApplicationController
  def index
  end

  def new
    @post = Post.new
  end
end

in new.html.erb, i have the following:

<h1>New Post</h1>

<% form_for :post do |f| %>

    <%= f.text_field :title %>

<% end %>

i noticed that in the scaffold generated code, the use the instance variable @post for the form_for helper. why do they use the instance variable in the scaffold generated form if passing the symbol :post in form_for does the exact same thing, while a symbol requires changing the config of the routes?

thank you very much, yuval

like image 312
Yuval Karmi Avatar asked Jun 05 '09 17:06

Yuval Karmi


People also ask

What is an instance variable Ruby on Rails?

What's an instance variable? In the Ruby programming language, an instance variable is a type of variable which starts with an @ symbol. Example: @fruit. An instance variable is used as part of Object-Oriented Programming (OOP) to give objects their own private space to store data.

Why symbols are used in Ruby on Rails?

Since a given symbol name refers to the same object throughout a Ruby program, Symbols are useful and more efficient than strings. Two strings with the same contents are two different objects, but for any given name, there is only one Symbol object. This can save both time and memory.

What does the mean before a variable in Ruby?

The @ symbol before a variable tells Ruby that we are working with an instance variable, and @@ before a variable tells us we are working with a class variable. We use @ before a variable in instance methods within a class to tell Ruby to access that attribute (instance variable) of the instance.


2 Answers

if you use symbol :post it creates

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

if you use the instance @post

for @post = Post.new you will get

<form action="/posts/create" class="new_account" id="new_account" method="post">

for @post = Post.find(1) you will get

<form action="/posts/update" class="edit_account" id="edit_account_1" method="post">
<input name="_method" type="hidden" value="put">

if you have different forms for your new and your edit no big deal but more likey than not your new and your edit forms are going to be identical or close to it

so if you use the instance variable @post you can put all the form code into _form and just call the partial and it will handle the rest based on if you pass in a new record or an existing record

like image 148
ErsatzRyan Avatar answered Oct 12 '22 21:10

ErsatzRyan


One possible reason is that it makes the code for the form to create a new post more similar to the code for a form to update an existing post.

like image 44
Matt Gillooly Avatar answered Oct 12 '22 21:10

Matt Gillooly