i created a scaffolded application in rails by the name of product. The product_controller.rb file contains the following.
class ProductsController < ApplicationController def new @product = Product.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @product } end end def create @product = Product.new(params[:product]) respond_to do |format| if @product.save flash[:notice] = 'Product was successfully created.' format.html { redirect_to(@product) } format.xml { render :xml => @product, :status => :created, :location => @product } else format.html { render :action => "new" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end Now when the url http://localhost:3000/products/create is given
Where new product link is clicked, control is transferred to new definition in the controller class and then an instance variable @product is created. BUT WHERE IS THIS VARIABLE PASSED? The function in turn calls new.rhtml which contains
<% form_for(@product) do |f| %> #all form elements declaration <% f.submit "Create" %> <%= end %>Here @product is initialized in the controller file and passed to this new.rhtml. So where does form_for(@product) gets the data?
How does the control gets transfered to create function in controller file when submit button is clicked? No where action is specified to the controller file.
in the create function, what does redirect_to(@product) specify where @product is an object received from the new.html file...
I am very much confused on the basics of ROR. Someone please help me clarify this. pardon me for making such a big post. I have lots of doubts in this single piece of code