My first Ruby on Rails + MongoDB Program

Source: Internet
Author: User
Tags ruby on rails

I want to learn more about MongoDB recently, but I used Ror a long time ago, and I just had a chance to pick it up again. The following is the process of creating the first project.

References:

1. rails 3-getting started

2. MongoDB and MongoDB mapper (you may need to flip the wall)

3. Getting started with Vmware cloudfoundry, MongoDB and rails (may need to go through the wall)

I. CreateProgramFramework

Rails active_record support is no longer used when creating a project:

 
$ Rails new todo-o

Complete the steps shown in document 1 in sequence.

1. Edit gemfile and add the following content:

 
Source "http://gemcutter.org" Gem "mongo_mapper"

2. Run

$ Bundle install

Install the project dependency package

3. Create a mongo. RB file under config/initializer to specify the global database information:

 
Extends mapper. connection = Mongo: connection. new ('localhost', 27017) extends er. database = 'todo '# For simplicity, different data is not specified for different environments if defined? (Phusionpassion) phusionpassenger. on_event (: starting_worker_process) Do | forked | extends Mapper. Connection. Connect if forked endend

4. Create a mongo. Rake file under the lib directory (not used in this article)

 
Namespace: DB do namespace: test do task: Prepare do # Stub Out for MongoDB end endend

After completing the preceding steps, start the program:

$ Rails server ** notice: C extension not loaded. this is required for optimum MongoDB Ruby driver performance. you can install the extension as follows: Gem install bson_ext if you continue to receive this message after installing, make sure that the bson_ext gem is in your load path and that the bson_ext and Mongo gems are of the same version. => booting webrick => rails 3.0.10 application starting in development on http: // 0.0.0.0: 3000 => call with-D to detach => Ctrl-C to shutdown server [23:36:14] info webrick 1.3.1 [23:36:14] info Ruby 1.9.2) [x86_64-linux] [23:36:14] info webrick: httpserver # Start: pid = 19595 Port = 3000

The above output shows that the bson_ext library is not loaded. First, follow the prompts to install the Library:

 
Sudo gem install bson_ext

Edit gemfile and add the following line:

Gem "bson_ext"

Start the program again. The Notice prompts that the message disappears and the startup is normal. Enter http: // 127.0.0.1: 3000 in the browser to view the following page:

2. Add pages and processing logic

Generate page, Controller, and model layer files using the generate command of rails:

 
$ Rails generate scaffold Project name: String -- ORM = mongo_mapper

After the command is executed, restart the server and enter http: // 127.0.0.1/projects in the browser. You can see the following:

Click the New Project LINK to add a new project:

Enter the project name and click Create project to save

Click Back to return to the List page. The newly created Project blog is displayed in the list:

Log on to the MongoDB database to query the inserted data:

So far, no row has been written.Code. But come here first, and continue later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.