日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Rubyform的兩種寫(xiě)法

下面介紹Ruby form的兩種寫(xiě)法。

Ruby form寫(xiě)法一:使用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 %> 

來(lái)看看解釋
 
引用

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.

而每個(gè)form的helper方法,如form.text_area,form_text_field,后面跟的符號(hào),如:name,:address,:email,等都是這個(gè)model的屬性。form_for后面跟的:order,就如dave所說(shuō),告訴方法這是一個(gè)實(shí)例變量。

接下來(lái)看看,我們?cè)诜椒ㄖ腥绾翁幚怼?/p>

 
 
 
  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 

如何得到這個(gè)實(shí)例變量order呢。

只用一句話(huà)

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

非常簡(jiǎn)潔。只要在html.erb頁(yè)面中配置好。

Ruby form寫(xiě)法二: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.  

來(lái)看解釋

引用

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的寫(xiě)法,沒(méi)有將屬性與model綁定起來(lái),而是直接寫(xiě)屬性名。每個(gè)helper方法都有兩個(gè)參數(shù),一個(gè)是域的名字,另一個(gè)是域的值。

來(lái)看在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 

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


本文標(biāo)題:Rubyform的兩種寫(xiě)法
URL鏈接:http://www.dlmjj.cn/article/cdsidje.html