我是 Rails 新手,我正在尝试学习这项技术,所以如果问题很愚蠢,请原谅。
我正在使用 Rails 3 。
请让我知道如何在数据库中插入记录。
我正在使用 postgresql,下面是学生表的表结构。
SELECT column_name FROM information_schema.columns WHERE table_name ='Students';
column_name
-------------
id
name
age
description
(4 rows)
这是我的控制器文件student_controller.rb
class StudentController < ApplicationController
def new
end
end
这是我的模型文件student.rb
class Student < ActiveRecord::Base
end
这是我在 \app\views\student\new.html.erb 下的视图文件
<h1>BookController#new</h1>
<form>
Id: <input type="text" name="id" /><br />
Last Name: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
Desciption: <input type="text" name="description" />
</form>
当我访问时http://localhost:3000/student/new
请让我知道如何在数据库中插入记录?
你了解RESTful吗?我假设您知道它,除非您可以在 Rails Guide 中找到它(在表单标签中,您必须添加@student,:action => :new, :method => :post
)
要添加新记录,只需键入Student.create(:name=> "a", :age => 2)
该声明由2句话组成
object = Student.new(:name => "a", :age => 2)
object.save
我建议你使用rails generate scaffold Student
相反,创建这样的一切。然后,阅读控制器、视图中的这些生成代码,你就会理解得很深刻!:)
P/s: 我也是业余爱好者:D
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)