Hi All,
Till now we have seen as how to setup a development environment and set up our database. Today I am gonna show you how we can build a small app say book strore
where we will add some data to the database and display it to the user.
To start with log on to mysql prompt.
# mysql -u linuxuser -p
#password linuxuser
mysql > use linuxcandy
mysql > describe books ;
If you have observed carefully I have created a table by name books.
The table is pretty simple. All it has is 3 fields . Book_id , name and an author field.
I hope you guys know how to create a table in mysql
. Now once you have created a table all we have to do is insert data to the table and to get a view of the same in the browser.
I have written the same in my development environment and it would not be feasible to show the screenshots and explain the code. What I would like is to give you the source code so that u can download it and use the same as the reference and continue. I would explain the main concepts in it and thus I guess we would be able to get a hold of the framework.
Please download the souce code of the app from the below link.
http://www.4shared.com/archive/p-3mEUqu/file.html
Untar the folder RailsFiles and put this folder in your document folder or whereever its feasible for you to work with
.
Open the file books_controller file which is in the controller folder .
There are many methods created like create, edit, update , delte and list.
Initially when you have start the server and type localhost:3000 the index page of this controller will be loaded.
Do the following change in the route.rb which is there in the config folder.
I have changed the map.connect , which tells the framework that whenever the server is started , the first page to be loaded should be from the books controller , index method.
Thus when you type localhost:3000 the list.rhtml page would be loaded and displyed in the browser. [A small question , If index.rhtml is the first page to load by default , then how is that list.rhtml is called ?
. Think about it.
].
Please see the list.rhtml in the books folder [src/app/views/books/list.rhtml] for the source code.
Click here to create a book would send a request back to the controller and the action name is create.
On click of create a book button the following page would be shown.
Now all we have done is displaying the create.rhtml file. Rails has certain convention which when followed makes our job easy
.
If you observe the in the create method , I have written @book_obj = Book.new . In Rails , this means book_obj is an instance of the Book model which actually maps to the table book that we created a while back in the mysql.
The object book_obj is been used in the create.rhtml . If you look at the source of create.rhtml you can see that I have used the ruby tag <% %>. So any ruby code that you need to write , you should use the <% %> tag. I have used the tag <%= text_field ‘book_obj’, ‘book_id’ %> , to create a text field. The first parameter is the name of the object and the second parameter is one of the field of that object. See how Ruby simplifies your job by setting the apporpriate value to the appropriate field.
So when you enter the data and click on submit , All the parameters are send the update method and all the parameters can be obtained by just using params [:book_obj][:book_id] for book_id and the same goes with name and author.
(Have a look at the update method of books_controller.)
So once the parameters are set all you have to do is save the object to the database. So its done by giving @book_obj.save …
. Once the object is saved ,I would redirect the page index.rhtml to display all the objects that were saved.
Book.find(:all) command gives you all the objects from the table Books. See how simple it is
.
Going forward we would continue to edit the contents of the table and to update the same .
We would also see how can we delete the contents from the Books table.
Please Note : As of now I have not included any java script validations. So even you just submit the
request null data will be stored in the book table.
.
Regards ,
Karthik L











