11

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 ??

2
  • as the answer will be awfully large for showing the complete procedure, please follow the Rails Getting Started. It will help you learn how Rails works. guides.rubyonrails.org/getting_started.html Commented Sep 19, 2012 at 7:28
  • @Preethi Jain: Did anything really helped you? Commented Sep 20, 2012 at 5:45

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

Only thing about this over create is it won't automatically generate a server id and will complain about id being nil.
7

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:

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.