[Ruby on Rails] and I learned the HelloWorld

Source: Internet
Author: User
Tags ruby on rails

1. Create a project
Rails New Blog

2. View the file structure below
Tree

The output is as follows, please pay attention to the part of the red circle.

Gemfile, a gems for managing applications, a bit like Python's package, has a dedicated website to find gems:https://rubygems.org/

App, (application) This will be your main energy place,

App/assets, this place is a picture, script, style and other static files.

App/controllers, App/models, App/views, these three are the so-called MVC c,m,v

CONFIG/ROUTES.RB, which is somewhat similar to the URL file in Django, configures the correspondence between the URL and the controller's actions.

DB/SEEDS.RB, the location of the database creation and migration (migration) file.

3. Add a controller named posts. Create a Controller

By convention: The Controller needs to be expressed in complex numbers, first into the engineering directory, and then create a controller, as follows

CD Blograils G Controller posts

Error encountered:

/usr/local/rvm/gems/ruby-2.1.5/gems/execjs-2.2.2/lib/execjs/runtimes.rb:51:in ' AutoDetect ': Could not find a JavaScript Runtime. See Https://github.com/sstephenson/execjs for a list of available runtimes. (execjs::runtimeunavailable)

error Reason: Coffeescript compiled into JavaScript requires JavaScript runtime, if not run, will error, prompting no EXECJS. Both Mac OS X and Windows generally provide the JavaScript runtime. In Rails-generated gemfile, the code that installs the Therubyracer Gem is commented out, and if you need to use the gem, remove the previous comment. It is recommended to use Therubyracer in JRuby. The gemfile that is generated in JRuby already contains this gem. See EXECJS for all supported runtimes.

Edit the Blog/gemfile file and uncomment it as follows

Gem ' Therubyracer ',  platforms:: ruby#enable it

Then run the sudo bindle install dependency. or directly

Gem Install Therubyracer

Gems are equivalent to Python's easy_install.

One more time. Generate controller, normal output is as follows:

      Create  app/controllers/posts_controller.rb      invoke  erb      create    app/views/posts      invoke  test_unit      Create    test/controllers/posts_controller_test.rb      invoke  helper      Create    app/helpers/posts_helper.rb      invoke    test_unit      create      Test/helpers/posts_helper_ TEST.RB      invoke  assets      invoke    coffee      Create      app/assets/javascripts/ Posts.js.coffee      invoke    scss      create      app/assets/stylesheets/posts.css.scss
Add Action action

Edit the App/controllers/posts_controller.rb file, add the index action (the action can be understood as the page), this is the default page to access the posts/path, as follows:

Class Postscontroller < Applicationcontroller  def index  endend

Add view

Define the action, it is equal to have an index page, next, you need to specify this page to render, under the App/views/posts path to create a new Index.html.erb (the default is the same name as the action named views, the Erb refers to embed Ruby, In this file you can use html/js/css/embed ruby).

Edit the contents as follows:

Add a route map

Edit the file: Config/routes.rb file, add the following route map:

Rails.application.routes.draw do  Resources:p OSTs

Running the service

Next run the emulation server:

Rails S

 

You can then access the path: http://localhost:3000/posts look at the output. Such as:

Reprint please specify this article from: http://www.cnblogs.com/Tommy-Yu/p/4140273.html, thank you!

[Ruby on Rails] and I learned the HelloWorld

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.