Pawan
Pawan

Reputation: 32321

Rails 3 : How to insert Record in Database using Rails

I am new to Rails , i am trying to learn this technology , so please excuse if the question is dumb .

I am using Rails 3 .

Please let me know how can i insert a Record in the Database .

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)

This is my Controller file student_controller.rb

class StudentController < ApplicationController

  def new
  end

end

This is my Model file student.rb

class Student < ActiveRecord::Base

end

This is my view file under \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>

When i access http://localhost:3000/student/new

Please let me know how can i insert a Record in Database ??

Upvotes: 11

Views: 40022

Answers (3)

Samiron
Samiron

Reputation: 5317

First of all you should use rails helper method form_for to generate build the form. Follow this link. In your model you should receive your student data as a hash in a key named student. So in your controller it will be like

def create
    @student = Student.new(params[:student])
    respond_to  do |format|
          .. ... ...
          #handle the response
    end
end

Here is a sample comments_controller.rb file for a quick look. https://gist.github.com/3748175


BUT MOST IMPORTANTLY!!

As you are completely new to this technology i would suggest to make a scaffold of a sample rails application and go through the automatically generated code.

# run this command in your command line to generate the codes
rails generate scaffold Student name:string age:integer description:text

Get more insights here.

Some Most Useful Links:

Upvotes: 7

duykhoa
duykhoa

Reputation: 2302

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

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

Upvotes: 15

ksol
ksol

Reputation: 12235

Rails is a complex framework. That does not mean it's hard (even if it is sometimes), but that there's a lot of topics to get your grasp on. You should definitely read a tutorial to help you get started : the officiel rails guide "Getting Started" is a very decent way to immerge yourself in rails.

After that, you'll have the answer to your question, but also more answers... and more questions too, probably.

Upvotes: 2

Related Questions