Rails 表单创建与元素使用全解析
1. 数据库迁移与表单基础
在开始创建表单之前,需要先进行数据库迁移。可以在文本编辑器中设置命令,仔细检查后粘贴执行。例如,查看在 db/migrate/ 目录下生成的迁移文件中的 self.up 方法,以下是创建 people 表的示例代码:
def self.up
create_table :people do |t|
t.string :name
t.string :secret
t.string :country
t.string :email
t.text :description
t.boolean :can_send_email
t.integer :graduation_year
t.float :body_temperature
t.decimal :price
t.date :birthday
t.time :favorite_time
t.timestamps
end
end
运行 rake db:migrate 命令,迁移将为应用程序构建数据库表。
接下来查看 Rails 为创建新人员生成的表单文件 app/views/people/new.html.erb ,以下是该文件的主要内容:
超级会员免费看
订阅专栏 解锁全文
31

被折叠的 条评论
为什么被折叠?



