自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Ruby form的兩種寫法

開發(fā) 開發(fā)工具
本文介紹兩種Ruby form的寫法:form_for以及form_tag。

下面介紹Ruby form的兩種寫法。

Ruby form寫法一:使用form_for

  1. < % form_for :order, :url => { :action => :save_order } do |form| %> 
  2.   < p> 
  3.    < %= label :order, :name, "Name:" %> 
  4.    < %= form.text_field :name, :size => 40 %>   
  5.   < /p> 
  6.   < p> 
  7.    < %= label :order, :address, "Address:" %> 
  8.    < %= form.text_area :address, :rows => 3, :cols => 40 %> 
  9.   < /p> 
  10.   < p> 
  11.    < %= label :order, :email, "E-Mail:" %> 
  12.    < %= form.text_field :email, :size => 40 %> 
  13.   < /p> 
  14.   < %= submit_tag "Place Order" , :class => "submit" %> 
  15. < % end %> 

來看看解釋
 
引用

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.

而每個form的helper方法,如form.text_area,form_text_field,后面跟的符號,如:name,:address,:email,等都是這個model的屬性。form_for后面跟的:order,就如dave所說,告訴方法這是一個實例變量。

接下來看看,我們在方法中如何處理。 

  1.  def save_order  
  2.   @cart = find_cart  
  3.   @order = Order.new(params[:order])  
  4.   @order.add_line_items_from_cart(@cart)  
  5.   if @order.save  
  6.     session[:cart] = nil 
  7.     redirect_to_index("Thank you for your order")  
  8.   else 
  9.     render :action=>:checkout 
  10.   end 
  11. end 

如何得到這個實例變量order呢。

只用一句話 

  1. @order = Order.new(params[:order])  

非常簡潔。只要在html.erb頁面中配置好。

Ruby form寫法二:form_tag

  1. < % form_tag do %> 
  2. < p> 
  3. < label for="name">Name:< /label> 
  4. < %= text_field_tag :name, params[:name] %> 
  5. < /p> 
  6. < p> 
  7. < label for="password">Password:< /label> 
  8. < %= password_field_tag :password, params[:password] %> 
  9. < /p> 
  10. < p> 
  11. < %= submit_tag "Login" %> 
  12. < /p> 
  13. < % end %> 
  14.  

來看解釋

引用

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 would be to have the controller set instance variables.

form_tag的寫法,沒有將屬性與model綁定起來,而是直接寫屬性名。每個helper方法都有兩個參數(shù),一個是域的名字,另一個是域的值。

來看在controller里如何處理

  1. def login  
  2.    user = User.authenticate(params[:name], params[:password])  
  3.    if user  
  4.       session[:user_id] = user.id  
  5.       redirect_to(:action => "index" )  
  6.    else 
  7.       flash.now[:notice] = "Invalid user/password combination" 
  8.    end 
  9. end 

這次在頁面中因為沒有將屬性與model綁定,所以也不能像form_for那樣,直接生成一個model,但是可以取值。取值時,可以取頁面中form的helper方法的第二個參數(shù)。(這樣不又跟jsp有些像了么)

【編輯推薦】

  1. 程序員們,是時候開始學習Ruby了
  2. 牛人點評Ruby語言十大令人喜愛的特點
  3. Python和Ruby:流行動態(tài)腳本語言之特點對比
  4. Ruby和Python的語法比較
  5. Ruby使用心得匯總:尋找高效的實現(xiàn)
責任編輯:yangsai 來源: JavaEye博客
相關推薦

2010-09-02 16:46:18

SQL刪除

2020-07-23 08:18:27

C語言執(zhí)行循環(huán)體條件

2010-10-11 10:31:51

MySQL分區(qū)

2013-05-27 14:31:34

Hadoop 2.0

2021-05-27 10:57:01

TCP定時器網(wǎng)絡協(xié)議

2021-11-16 06:55:36

Linux字符設備

2010-03-11 14:34:47

Python環(huán)境

2011-03-03 10:26:04

Pureftpd

2010-08-06 09:38:11

Flex讀取XML

2010-06-07 17:41:42

Sendmail 配置

2010-07-14 16:28:58

配線架

2023-03-29 13:06:36

2021-08-11 06:57:16

ShuffleSpark核心

2022-03-15 08:25:32

SparkShuffle框架

2024-06-06 08:32:52

.NET框架代碼

2011-04-06 12:41:41

Java異常

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2009-11-23 14:09:53

PHP的foreach

2010-09-07 11:09:59

點贊
收藏

51CTO技術棧公眾號