Is there a point to assigning so many instance variables in controllers? I’ve been reading some articles about TDD and general Rails stuff and I constantly see code like this:
class Admin::ProductsController < ApplicationController def index @products = Product.order(:title) end def new @product = Product.new end def create @product = Product.new(product_params) if @product.save redirect_to edit_admin_product_path(@product), notice: 'The product was created successfully.' end end # ... etc end
Instance variables in controllers, to my knowledge, are primarily for exposing content to views. So why in this example is the product being assigned to an instance variable in
ProductsController#create? Is it faster to assign something as an instance variable rather than a local variable? I’m confused by this and would appreciate some insight from someone. Thanks!