rails 之 form_for VS form_tag

本文详细介绍了Rubyform的两种写法,包括使用form_for和form_tag的方法,并解释了每种写法背后的实现原理。通过实例演示了如何在控制器中处理来自表单的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面介绍Ruby form的两种写法。

1. Ruby form写法一:使用form_for
    
< % form_for :order, :url => { :action => :save_order } do |form| %>
  < p>
   < %= label :order, :name, "Name:" %>
   < %= form.text_field :name, :size => 40 %>  
  < /p>
  < p>
   < %= label :order, :address, "Address:" %>
   < %= form.text_area :address, :rows => 3, :cols => 40 %>
  < /p>
  < p>
   < %= label :order, :email, "E-Mail:" %>
   < %= form.text_field :email, :size => 40 %>
  < /p>
  < %= submit_tag "Place Order" , :class => "submit" %>
< % 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所说,告诉方法这是一个实例变量。

接下来看看,我们在方法中如何处理。

def save_order
  @cart = find_cart
  @order = Order.new(params[:order])
  @order.add_line_items_from_cart(@cart)
  if @order.save
    session[:cart] = nil
    redirect_to_index("Thank you for your order")
  else
    render :action=>:checkout
  end
end

如何得到这个实例变量order呢。

只用一句话    
@order = Order.new(params[:order])

非常简洁。只要在html.erb页面中配置好。

 

2. Ruby form写法二:form_tag
    
< % form_tag {:action => :login} do %>
< p>
< label for="name">Name:< /label>
< %= text_field_tag :name, params[:name] %>
< /p>
< p>
< label for="password">Password:< /label>
< %= password_field_tag :password, params[:password] %>
< /p>
< p>
< %= submit_tag "Login" %>
< /p>
< % end %>

 

来看解释

引用

        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里如何处理    
def login
   user = User.authenticate(params[:name], params[:password])
   if user
      session[:user_id] = user.id
      redirect_to(:action => "index" )
   else
      flash.now[:notice] = "Invalid user/password combination"
   end
end

这次在页面中因为没有将属性与model绑定,所以也不能像form_for那样,直接生成一个model,但是可以取值。取值时,可以取页面中form的helper方法的第二个参数。(这样不又跟jsp有些像了么)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值