Ruby on rails 3 Rails 3:如何使用Rails在数据库中插入记录

Ruby on rails 3 Rails 3:如何使用Rails在数据库中插入记录,ruby-on-rails-3,Ruby On Rails 3,我是Rails新手,我正在尝试学习这项技术,所以如果问题是愚蠢的,请原谅 我使用的是Rails3 请告诉我如何在数据库中插入记录 我正在使用postgresql,下面是我的Students表的表结构 SELECT column_name FROM information_schema.columns WHERE table_name ='Students'; column_name ------------- id name age description (4 rows) 这是我

我是Rails新手,我正在尝试学习这项技术,所以如果问题是愚蠢的,请原谅

我使用的是Rails3

请告诉我如何在数据库中插入记录

我正在使用postgresql,下面是我的Students表的表结构

SELECT column_name FROM information_schema.columns WHERE table_name ='Students';

 column_name
-------------
 id
 name
 age
 description
(4 rows)
这是我的控制器文件student\u Controller.rb

class StudentController < ApplicationController

  def new
  end

end
class Student < ActiveRecord::Base

end
<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>
class StudentController
这是我的模型文件student.rb

class StudentController < ApplicationController

  def new
  end

end
class Student < ActiveRecord::Base

end
<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>
class-Student
这是我在\app\views\student\new.html.erb下的视图文件

class StudentController < ApplicationController

  def new
  end

end
class Student < ActiveRecord::Base

end
<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>
BookController#新增
Id:
姓氏: 年龄: 描述:
当我访问
http://localhost:3000/student/new


请告诉我如何在数据库中插入记录???

首先,您应该使用rails helper方法
form\u来生成表单。跟着。在您的模型中,您应该以散列形式接收名为
student
的键中的学生数据。所以在你的控制器里

def create
    @student = Student.new(params[:student])
    respond_to  do |format|
          .. ... ...
          #handle the response
    end
end
下面是一个示例
comments\u controller.rb
文件,供快速查看


但最重要的是

由于您对这项技术完全陌生,我建议您制作一个示例rails应用程序的框架,并完成自动生成的代码

# run this command in your command line to generate the codes
rails generate scaffold Student name:string age:integer description:text
获得更多的见解

一些最有用的链接:

  • :)

    • Rails是一个复杂的框架。这并不意味着这很难(即使有时很难),但有很多话题需要你去把握。您一定要阅读一篇教程来帮助您入门:Officel rails指南是一种让您沉浸在rails中的非常好的方式


      在那之后,你会有你的问题的答案,但也会有更多的答案。。。可能还有更多的问题。

      你了解RESTful吗?我假设您知道它,除非您可以在rails指南中找到它(在表单标记中,您必须添加
      @student,:action=>:new,:method=>:post
      ) 要添加新记录,只需键入
      Student.create(:name=>“a”,:age=>2)
      这句话由两句话组成

      object = Student.new(:name => "a", :age => 2)
      object.save
      
      我建议您使用
      rails-generate-scaffold-Student
      而不是像这样创建所有内容。然后,在控制器、视图中阅读这些生成代码,您将非常深刻地理解!)
      P/s:我也是一个业余爱好者:D

      因为答案对于显示完整的程序来说会非常大,请按照轨道开始。它将帮助您了解Rails的工作原理@Preethi Jain:真的有什么帮助吗?这种过度创建的唯一原因是它不会自动生成服务器id,并且会抱怨id为零。