成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Ruby form的兩種寫法

開發 開發工具
本文介紹兩種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方法都有兩個參數,一個是域的名字,另一個是域的值。

來看在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方法的第二個參數。(這樣不又跟jsp有些像了么)

【編輯推薦】

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

2010-09-02 16:46:18

SQL刪除

2020-07-23 08:18:27

C語言執行循環體條件

2010-10-11 10:31:51

MySQL分區

2013-05-27 14:31:34

Hadoop 2.0

2021-05-27 10:57:01

TCP定時器網絡協議

2011-03-03 10:26:04

Pureftpd

2021-11-16 06:55:36

Linux字符設備

2010-03-11 14:34:47

Python環境

2011-04-06 12:41:41

Java異常

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2024-06-06 08:32:52

.NET框架代碼

2010-08-06 09:38:11

Flex讀取XML

2021-08-11 06:57:16

ShuffleSpark核心

2010-06-07 17:41:42

Sendmail 配置

2023-03-29 13:06:36

2010-07-14 16:28:58

配線架

2022-03-15 08:25:32

SparkShuffle框架

2009-11-23 14:09:53

PHP的foreach

2010-02-24 14:25:48

WCF地址
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品国产综合久久香蕉麻豆 | 日韩av一区二区在线观看 | 二区成人 | 欧美日韩在线国产 | 国产在线播放av | 韩国毛片一区二区三区 | 国产精品久久久久久久久久久久 | 国产成人精品免费视频 | 久久一视频 | 午夜在线视频 | 精品一二区 | 天天久久 | 国产综合区 | 国产传媒毛片精品视频第一次 | 黑人精品xxx一区一二区 | 亚洲色图第一页 | 四虎影院美女 | 九九精品在线 | 国产一区二区三区久久久久久久久 | 国产午夜精品一区二区三区嫩草 | 美女天天操 | 亚洲天堂免费在线 | 91影院| 亚洲一区二区三区视频 | 日本三级在线视频 | 中文字幕综合 | 精品三级在线观看 | 国产99久久久久 | 日韩在线播放中文字幕 | 天天干视频 | 欧美一区二区精品 | 一区二区三区在线播放视频 | 欧美8一10sex性hd | 久久av一区 | 中文字幕av一区二区三区 | 2022国产精品 | 精品一区视频 | 天天爽夜夜爽精品视频婷婷 | 久久精品色欧美aⅴ一区二区 | 中文字幕国产高清 | 午夜影院在线观看 |