Monday, 30 January 2017
Wednesday, 15 July 2015
Tuesday, 5 May 2015
Wednesday, 17 September 2014
Say "Hello", Rails
4.2 Say "Hello", Rails
To get Rails saying "Hello", you need to create at minimum a controller and a view.A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.
A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the controller, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called eRuby (Embedded Ruby) which is processed by the request cycle in Rails before being sent to the user.
To create a new controller, you will need to run the "controller" generator and tell it you want a controller called "welcome" with an action called "index", just like this:
$ bin/rails generate controller welcome index |
create app/controllers/welcome_controller.rb route get 'welcome/index'invoke erbcreate app/views/welcomecreate app/views/welcome/index.html.erbinvoke test_unitcreate test/controllers/welcome_controller_test.rbinvoke helpercreate app/helpers/welcome_helper.rbinvoke test_unitcreate test/helpers/welcome_helper_test.rbinvoke assetsinvoke coffeecreate app/assets/javascripts/welcome.js.coffeeinvoke scsscreate app/assets/stylesheets/welcome.css.scss |
app/controllers/welcome_controller.rb
and the view, located at app/views/welcome/index.html.erb.Open the
app/views/welcome/index.html.erb file in your text editor. Delete all
of the existing code in the file, and replace it with the following single line
of code:<h1>Hello, Rails!</h1>Hello, Rails!
4 Hello, Rails!
To begin with, let's get some text up on screen quickly. To do this, you need to get your Rails application server running.4.1 Starting up the Web Server
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running the following in theblog directory:$ bin/rails server |
Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the
absence of a runtime will give you an
This will fire up WEBrick, a web server distributed with Ruby by default. To see
your application in action, open a browser window and navigate to
http://localhost:3000. You should see the Rails default information page:execjs error. Usually Mac OS X and
Windows come with a JavaScript runtime installed. Rails adds the therubyracer
gem to the generated Gemfile in a commented line for new apps and you can
uncomment if you need it. therubyrhino is the recommended runtime for JRuby
users and is added by default to the Gemfile in apps generated under JRuby.
You can investigate about all the supported runtimes at
ExecJS.
To stop the web server, hit Ctrl+C in the terminal window where it's
running. To verify the server has stopped you should see your command prompt
cursor again. For most UNIX-like systems including Mac OS X this will be a
dollar sign
The "Welcome aboard" page is the smoke test for a new Rails application: it
makes sure that you have your software configured correctly enough to serve a
page. You can also click on the About your application's environment link to
see a summary of your application's environment.$. In development mode, Rails does not generally require you to
restart the server; changes you make in files will be automatically picked up by
the server.Creating the Blog Application
3.2 Creating the Blog Application
Rails comes with a number of scripts called generators that are designed to make your development life easier by creating everything that's necessary to start working on a particular task. One of these is the new application generator, which will provide you with the foundation of a fresh Rails application so that you don't have to write it yourself.To use this generator, open a terminal, navigate to a directory where you have rights to create files, and type:
$ rails new blog |
blog directory and
install the gem dependencies that are already mentioned in Gemfile using
bundle install.
You can see all of the command line options that the Rails application
builder accepts by running
After you create the blog application, switch to its folder:rails new -h.$ cd blog |
blog directory has a number of auto-generated files and folders that make
up the structure of a Rails application. Most of the work in this tutorial will
happen in the app folder, but here's a basic rundown on the function of each
of the files and folders that Rails created by default:| File/Folder | Purpose |
|---|---|
| app/ | Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide. |
| bin/ | Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application. |
| config/ | Configure your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications. |
| config.ru | Rack configuration for Rack based servers used to start the application. |
| db/ | Contains your current database schema, as well as the database migrations. |
| Gemfile Gemfile.lock |
These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website. |
| lib/ | Extended modules for your application. |
| log/ | Application log files. |
| public/ | The only folder seen by the world as-is. Contains static files and compiled assets. |
| Rakefile | This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application. |
| README.rdoc | This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on. |
| test/ | Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications. |
| tmp/ | Temporary files (like cache, pid, and session files). |
| vendor/ | A place for all third-party code. In a typical Rails application this includes vendored gems. |
Subscribe to:
Comments (Atom)