Rails 3:如何使用 Rails 在数据库中插入记录 [英] Rails 3 : How to insert Record in Database using Rails
问题描述
我是 Rails 的新手,我正在努力学习这项技术,所以如果问题很愚蠢,请原谅.
I am new to Rails , i am trying to learn this technology , so please excuse if the question is dumb .
我使用的是 Rails 3.
I am using Rails 3 .
请告诉我如何在数据库中插入记录.
Please let me know how can i insert a Record in the Database .
我正在使用 postgresql ,下面是我的学生表的表结构.
I am uisng postgresql , and below is my Table structure for the Students Table .
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
请告诉我如何在数据库中插入记录??
Please let me know how can i insert a Record in Database ??
推荐答案
你了解 RESTful 吗?我假设你知道它,除非你能在 rails guide 中找到它(在表单标签中,你必须添加 @student,:action => :new, :method => :post
)要添加新记录,只需输入 Student.create(:name=> "a", :age => 2)
这个语句由2个句子组成
Do you understand about RESTful? I assume that you know it, unless you can find it in rails guide (In form tag, you must add @student,:action => :new, :method => :post
)
To add new record, just type Student.create(:name=> "a", :age => 2)
This statement is composed of 2 sentences
object = Student.new(:name => "a", :age => 2)
object.save
我建议你使用 rails generate scaffold Student
而不是像这样创建一切.然后,在controller,views中阅读这些generate代码,你会理解的很深!:)P/s: 我也是业余的:D
I suggest you use rails generate scaffold Student
instead create everything like this. And then, read these generate code in controller, views, you will understand very deeply!:)
P/s: i am an amateur too:D
这篇关于Rails 3:如何使用 Rails 在数据库中插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!