ruby on rails basics help

Posted by CHID on Stack Overflow See other posts from Stack Overflow or by CHID
Published on 2011-01-01T16:43:53Z Indexed on 2011/01/01 16:54 UTC
Read the original article Hit count: 174

Filed under:

Hi, 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

  1. 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 funtion inturn calls new.rhtml which contains

        <% form_for(@product) do |f| %>
        #all form elments declaration
        <% f.submit "Create" %>
        <%= end %>
    

  2. Here @product is initialized in the controller file and passed to this new.rhtml. So where does form_for(@product) gets the data?

  3. How does the control gets tranfered to create function in controller file when submit button is clicked? No where action is specified to the controller file.

  4. in the create function, wat does redirec_to(@product) specify where @product is an object received from the new.html file...

I am very much confused on the basics of ROR. Somone pls help me clarify this. pardon me for making such a big post. I have lots of doubts in this single piece of code

© Stack Overflow or respective owner

Related posts about ruby-on-rails