Rails form_for vs form_tag

Source: Internet
Author: User

The following describes two methods of writing Ruby form.

1. Ruby form: Use form_for

 
<% Form_for: Order,: URL =>{: action =>: save_order} Do | form |%> <p ><%= label: Order,: name, "Name: "%> <% = form. text_field: name,: size => 40%> </P> <p> <% = label: Order,: address, "Address:" %> <% = form. text_area: Address,: rows => 3,: Cols => 40%> </P> <p> <% = label: Order,: email, "E-mail: "%> <% = form. text_field: E-mail,: size => 40%> </P> <% = submit_tag "place order",: class => "Submit" %> <% end %>

Let's look at the explanation.
Reference

There are two interesting things in this Code. first, the form_for helper on Line 1 sets up a standard HTML form. but it does more. the first parameter,: Order, tells the method that it's dealing with an object in an instance variable named @ order. the Helper uses this information when naming fields and when arranging for the field values to be passed back to the Controller.

The: URL parameter tells the Helper what to do when the user hits the submit button. in this case, we'll generate an http post request that'll end up getting handled by the save_order action in the controller.

The Helper method of each form, such as form. text_area, form_text_field, and the following symbol, such as: name,: Address,: email, are all attributes of this model. Order followed by form_for, as Dave said, tells the method that this is an instance variable.

Next, let's take a look at how we can handle it in the method.

Def save_order @ cart = find_cart @ order = order. new (Params [: Order]) @ order. add_line_items_from_cart (@ cart) If @ order. save session [: cart] = nil redirect_to_index ("Thank you for your order") else render: Action =>: checkout end

How can we get the order variable of this instance.

Just one sentence

 
@ Order = order. New (Params [: Order])

Very concise. You only need to configure it on the HTML. ERB page.

2. Ruby form: form_tag

 
 
<% Form_tag {: Action =>: Login} Do %> <p> <label for = "name"> name: </label> <% = text_field_tag: Name, params [: Name] %> </P> <p> <label for = "password"> password: </label> <% = password_field_tag: Password, Params [: password] %> </P> <p> <% = submit_tag "login" %> </P> <% end %>

 

Let's look at the explanation.

Reference

This form is different from ones we 've seen earlier. rather than using form_for, it uses form_tag, which simply builds a regular HTML <form>. inside that form, it uses text_field_tag and password_field_tag, two helpers that create HTML <input> tags. each helper takes two parameters. the first is the name to give to the field, and the second is the value with which to populate the field. this style of form allows us to associate values in the Params structure directly with form fields-no model object is required. in our case, we chose to use the Params object directly in the form. an alternative wocould be to have the Controller set instance variables.

The form_tag syntax does not bind the attribute to the model, but directly writes the attribute name. Each helper method has two parameters: one is the name of the domain and the other is the value of the domain.

Let's see how to deal with it in the controller.

Def Login User = user. authenticate (Params [: name], Params [: Password]) If user session [: user_id] = user. id redirect_to (: Action => "Index") else flash. now [: Notice] = "invalid user/password combination" end

This time, because the attribute is not bound to the model, you cannot directly generate a model like form_for, but you can set the value. When the value is set, you can take the second parameter of the form helper method on the page. (Isn't it like JSP again)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.