日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

下面介紹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.

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

接下來看看,我們?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呢。

只用一句話

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

來看在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àn)闆]有將屬性與model綁定,所以也不能像form_for那樣,直接生成一個(gè)model,但是可以取值。取值時(shí),可以取頁面中form的helper方法的第二個(gè)參數(shù)。(這樣不又跟jsp有些像了么)


文章標(biāo)題:Rubyform的兩種寫法
轉(zhuǎn)載來于:http://m.5511xx.com/article/cdsidje.html