开发者

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开发者_JAVA技巧 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


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.


  1. The data is passed using HTML POST parameters.

  2. form_for(@product) is just a form helper, which generates the proper HTML form with the appropriate actions.

    More information: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

  3. The action is specified in form_for.

  4. redirect_to(@product) redirects you to the newly created product. It does this by looking up the route to the Product controller, using the id contained within @product.

    More on routing can be found here: http://edgeguides.rubyonrails.org/routing.html


As a user, you would never go to the /products/create path. That's the destination for the submit button. When you go to products/new, the default values for the variables used in the new.html.erb template. In this case, @product is being created.

When you hit the submit button, it goes to /products/create, passing the contents of the form that was generated to it as params. Rails is taking the params and assigning the values to a new product (using something called Mass Assignment).

Rails is very much "convention over configuration". It assumes that when you click the submit button from the /products/new view that you're creating an object, so it knows to go to create. This is all defined by REST, something you should read up on.

Finally, redirect_to is a super-smart function that knows if an object is being passed to it, you want to go to the show view for that product.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜