Rail based rubies

i created a stand in rails called 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 that url is set http://localhost:3000/products/create

  • When a new product link is clicked, control is passed to the new definition in the controller class, and then the @product instance variable is created. BUT WHERE DOES THIS CHANGE PASS? 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) get the data?

  • How is control transferred to create a function in the controller file when the submit button is clicked? No, where the action of the controller file is indicated.

  • in the create function, that redirect_to (@product) indicates where @product is the object obtained from new.html ...

I am very confused by the basics of ROR. Someone please help me clarify this. excuse me for such a great post. I have many doubts about this particular piece of code.

+4
source share
3 answers

Wow, that’s a lot of questions. First, let me recommend you take a copy of " Beginning Rails 3 ", which is a fantastic introduction to Rails that will answer all these questions and help you quickly become a very reliable rail programmer.

Secondly, here are some basic answers for you:

1) You do not have to browse products/create , you just browse products/new . Whenever you look at the URL, you send a GET request. The "new" action is waiting for a GET request, but the CREATE action is waiting for a POST request. POST requests are generated by submitting forms.

Therefore, the stream looks like this:

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

The relationship between NEW and CREATE is reflected in EDIT and UPDATE. That is, to change the object you are viewing on products/123/edit , and from there you submit a form that triggers the UPDATE action.

All of this falls under the so-called RESTful design, which really is the heart of how Rails works. You can learn more about REST, here is a good place to start .

2) form_for receives data from the controller, but in the case of the NEW action it does not receive data, but only an empty (new) object. form_for is the helper that receives the object, and from this object defines some HTML code that must occur in order for the formed forms to interact correctly with your controller.

The same thing happens when loading a page in products/edit , but the difference is that if you pass form_for existing (not new) object, it will fill the form fields with existing values ​​from the object.

3) Control transfer occurs through an HTTP request configured in the HTML <form> . This is part of the “magic” of rails, it controls the connection between the browser and the controllers for you, so you don’t need to worry about it.

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

Hope this helps, but please consider putting together the book “The Beginning of Rails”: it’s very good, you can go through it in about a week, and you will save time from time to time than wandering around a code like this, which you are completely unfamiliar.

+7
source
+2
source

As a user, you will never follow the path /products/create . This is 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 created.

When you click the submit button, it will go to /products/create , passing in the contents of the form that was generated for it as params . Rails takes parameters and assigns values ​​to the new product (using something called Mass Assignment).

Rails is a very "configuration convention". It is assumed that when you click the Submit button from the /products/new view that you create for an object, it knows what you need to create. All of this is determined by the REST that you should read.

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

0
source

Source: https://habr.com/ru/post/1333945/


All Articles