Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails basics help

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 function in turn calls new.rhtml which contains

    <% form_for(@product) do |f| %>
    #all form elements 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 transfered 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, 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

like image 990
CHID Avatar asked Jan 21 '23 20:01

CHID


1 Answers

Wow, that's a lot of questions. First, let me recommend you pick up a copy of "Beginning Rails 3", which is a fantastic introduction to Rails which would answer all of these questions and help you quickly become a very solid rails programmer.

Second, here are some basic answers for you:

1) You shouldn't browse to products/create, you just browse to products/new. Whenever you browse to a URL you're sending a GET request. The "new" action expects a GET request, but the CREATE action expects a POST request. POST requests are generated by submitting forms.

Therefore, the flow is like this:

The NEW action is used to create a form appropriate to the model in question (products). When you submit the form from products/new it will POST to products/create, which will trigger the code in the CREATE action.

The relationship between NEW and CREATE is mirrored in EDIT and UPDATE. Ie, to change an object you browse to products/123/edit, and from there you submit a form which triggers the UPDATE action.

This all falls under what is called "RESTful" design, which is really the heart of how Rails works. You may want to learn more about REST, here's a good place to start.

2) form_for gets data from the controller -- but in the case of the NEW action it isn't getting data, just an empty (new) object. form_for is a helper which receives an object and from that object determines some of the HTML that needs to happen in order for the forms that are generated to correctly interact with your controller.

The same thing happens when you load a page at products/edit, but the difference is if you pass form_for an existing (not new) object, it will populate the fields of the form with the existing values from the object.

3) The transfer of control is happening via the HTTP request as set up in the HTML <form> tag. This is part of the 'magic' of rails, it handles the linkages between the browser and the controllers for you so you don't have to worry about it.

4) I don't usually use redirect_to(@product), but I would expect it to take you to the page for the product you just created, ie: products/123 where 123 is the ID of the product.

I hope this helps, but please do consider picking up the Beginning Rails book: it's very very good, you can progress through it in about a week, and you'll save TONS of time by getting started on a solid foundation rather than wandering through code like this that is totally unfamiliar to you.

like image 132
Andrew Avatar answered Jan 30 '23 21:01

Andrew