Web Application Development Practical: Ruby on Rails - Working with DatabaseIn this practical, you will learn how to integrating databases into the cookbook application. Part I: Getting Up and Running Quickly with ScaffoldingRails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job. Step 1) Creating a ResourceIf you haven't created the cookbook project, or lost your code, you should first create a new project called cookbook as in previous practical:
If you have your cookbook project generated before, make sure you are in this folder after you fire up the terminal. Now we can start by generating a scaffolded Recipe resource: this will represent a single recipe posting. Open a terminal and enter this command:
See what files have been generated, and in particular pay attention to the following files: app/models/recipe.rb app/controllers/recipes_controller.rb app/views/recipes/* Also the following database related files: db/migrate/2012XXXXXXX_create_recipes.rb (XXXX stands for a series of number) db/schema.rb Open these files and try to understand them. Step 2) Running a MigrationOne of the products of the rails generate scaffold command is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration after it’s been applied to your database. At this point, you can use a rake command to run the migration:
This command line of code will bring the database table to life. It defines actions for all CRUD operations, immediately allowing us to create, read, update, and delete recipes in our database! Step 3) The Excitement Begins
Open a browser and navigate to http://127.0.0.1:3000/recipes/new. You should see something like Figure 1.
Figure 4. A listing of all recipes Then, click on "Back" you will see the list of recipes in your database (Figure 4). Next, try to add another one by clicking the "New recipe" link and entering the data, as in Figure 5.
Part II: Searching the databaseIn this part we will learn how to search the database if a user types in a keyword. Step 4) Search recipe on the databaseEdit the "application.rb" file under cookbook\app\controllers\ and comment out the following line: "protect_from_forgery". Edit the " index.html.erb" file under cookbook\app\views\recipes\ and add following code at the end of file. <h3>Enter keyword</h3> Edit the "recipes_controller.rb" file under cookbook\app\controllers\ and create the following method. def search In the directory cookbook\app\views\recipe, create a file named search.html.erb containing the following: <html> Refresh your browser and try to use the search function that we have created then observe the result. You will see the below routing error as we haven't update the routing file yet.
Open config/routes.rb and modify the file as follows:
If you don't understand the code in config/routes.rb, go to this link to check further details about Rail Routing (especially Section 2.9). Now try to use the search function again. In the meantime, play around with adding, deleting, and editing recipes. Go ahead. Part IIIYou are required to develop a simple web application called "To-do List" and its database.
|