Ruby form的两种写法
下面介绍Ruby form的两种写法。
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_f
相关文档:
转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
分页中用到类变量,主要是用来标记“页码输入框”的id 如果一个页面有几个分页,“页码输入框”的id要是不同的才能分清是哪个要分页。使用类变量就是为了达到这个目的,让所有的对象实例共用一个变量,不必每次重新初始化变量。 类变量使用代码示例 1 require 'ruby-debug'
2 debugger
3 cla ......
我们在使用C编程时会遇到一个问题,比如头文件的一个函数包含在一个lib中,但是
在实际连接中我们不知道它在哪个库中。也许可行的一种办法是直接上网查询某个
函数的依赖条件,这对于常用函数是没问题的!但是对于复杂而又缺少文档的第三方
lib来说,无异于大海捞针。
自此通过2种办法来尝试解决这个问题,我们先看第一 ......
开发环境
OS:WindowsXP
Ruby: Ruby1.9.1
Rails: Rails2.3.5
IDE: RubyMine2.0.1
1、创建Rails工程
2、修改 /config/database.yml
自动创建的工程中默认数据库连接的是sqlite,如果没有安装此数据库,需要修改该配置(本例中使用的是mysql)
# Mysql Version 5.1.46
development:
adapter: mysql
database: ......
开发环境:
OS:Windows XP
Ruby:Ruby1.9.1
Rails:Rails2.3.5
will_paginate:will_paginate2.3.11
(在命令行中运行 gem install mislav-will_paginate --source http://gems.github.com )
IDE:Rubymine2.0.1
DB:mysql5.0.9
本例在上一个例子(Ruby实践—简单数据库操作)的基础上实现分页,利用的是will_p ......