成人免费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字符設備

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-03-11 14:34:47

Python環境

2009-11-23 14:09:53

PHP的foreach

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框架

2010-02-24 14:25:48

WCF地址
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩成人免费 | 精品国产乱码久久久久久1区2区 | 一色桃子av一区二区 | 国产欧美日韩在线观看 | 国产精品美女久久久久久免费 | 欧美一级片在线播放 | 欧美日韩一区在线播放 | 日本黄色不卡视频 | a黄毛片 | 99成人| 欧美日韩久久久久 | 欧美男人天堂 | 成在线人视频免费视频 | 真人女人一级毛片免费播放 | 久久久国产精品 | 午夜大片 | 国产精品久久久久久久久久免费看 | 国产一区二区三区 | 日本电影免费完整观看 | 欧美在线视频不卡 | 免费观看黄色一级片 | 伊人婷婷| av影音资源| 天堂一区在线观看 | 五月激情婷婷六月 | 久久中文高清 | 色吧综合网| 在线日韩 | 国产精品一二三区 | 天堂中文在线播放 | www.av在线| 国产视频三级 | 另类专区亚洲 | jlzzxxxx18hd护士| 免费成人av网站 | 在线看片国产精品 | 一级片av | 成人三级视频 | 在线视频一区二区 | 国产美女久久久 | 国产精品久久久久久吹潮 |