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博客
相关推荐

2020-07-23 08:18:27

C语言执行循环体条件

2010-09-02 16:46:18

SQL删除

2021-11-16 06:55:36

Linux字符设备

2010-03-11 14:34:47

Python环境

2010-10-11 10:31:51

MySQL分区

2013-05-27 14:31:34

Hadoop 2.0

2010-08-06 09:38:11

Flex读取XML

2010-06-07 17:41:42

Sendmail 配置

2021-08-11 06:57:16

ShuffleSpark核心

2023-03-29 13:06:36

2010-07-14 16:28:58

配线架

2022-03-15 08:25:32

SparkShuffle框架

2021-05-27 10:57:01

TCP定时器网络协议

2011-03-03 10:26:04

Pureftpd

2011-04-06 12:41:41

Java异常

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2010-09-07 11:09:59

2009-09-08 15:22:20

Spring依赖注入

2011-04-02 09:48:38

深拷贝
点赞
收藏

51CTO技术栈公众号