Rails 3: How to insert a record into a database using Rails

I'm new to Rails, I'm trying to learn this technology, so please excuse me if the question is dumb.

I am using Rails 3.

Please let me know how I can insert a record into the database.

I am uisng postgresql and below is my table structure for student table.

SELECT column_name FROM information_schema.columns WHERE table_name ='Students'; column_name ------------- id name age description (4 rows) 

This is my student_controller.rb controller file

 class StudentController < ApplicationController def new end end 

This is my student.rb model file

 class Student < ActiveRecord::Base end 

This is my file with the view in \ 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 into the database?

+6
source share
3 answers

Do you understand about RESTful? I assume that you know this if you cannot find it in rails guides (you must add @student,:action => :new, :method => :post in the form tag @student,:action => :new, :method => :post ) To add a new entry, just type Student.create(:name=> "a", :age => 2) This statement consists of 2 sentences

 object = Student.new(:name => "a", :age => 2) object.save 

I suggest you use rails generate scaffold Student instead to create everything like this. And then, read these generate code in the controller, look, you will understand very deeply! :) P / s: I am also an amateur: D

+12
source

First of all, you should use the hells form_for method to create the form. Follow this link . In your model, you should get your student data as a hash in a key called 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 an example comments_controller.rb file for quick viewing. https://gist.github.com/3748175


BUT THE MOST IMPORTANT !!

Since you are completely unfamiliar with this technology, I would suggest to make a scaffold of a sample application-rails and go through an 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 info here .

Some of the most useful links:

+7
source

Rails is a complex structure. This does not mean that it is difficult (even if it is sometimes), but there are many topics to understand your understanding. You should definitely read the tutorial that will help you get started: the Getting Started Ads Serving Guide is a very decent way to get on the rails.

After that, you will have the answer to your question, but even more answers ... and even more questions, perhaps.

+2
source

Source: https://habr.com/ru/post/925743/


All Articles