c# barcode generator wpf The app/controllers/salutation_controller.rb File in Font

Creator QR-Code in Font The app/controllers/salutation_controller.rb File

Listing 2-1. The app/controllers/salutation_controller.rb File
QR Code JIS X 0510 Generation In None
Using Barcode creator for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
Print PDF-417 2d Barcode In None
Using Barcode creation for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
class SalutationController < ApplicationController end
UPC Symbol Drawer In None
Using Barcode creation for Font Control to generate, create Universal Product Code version A image in Font applications.
www.OnBarcode.com
QR Code JIS X 0510 Generator In None
Using Barcode creator for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
Creating an Action
Printing EAN 13 In None
Using Barcode generator for Font Control to generate, create GS1 - 13 image in Font applications.
www.OnBarcode.com
Generating Barcode In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Controllers respond to actions. If we want our SalutationController to respond to a request for hello, we ll need to make an action for it. Open salutation_controller.rb in your text editor and add the hello action, as shown in Listing 2-2.
Printing Code 128 Code Set C In None
Using Barcode generation for Font Control to generate, create Code 128B image in Font applications.
www.OnBarcode.com
Printing MSI Plessey In None
Using Barcode encoder for Font Control to generate, create MSI Plessey image in Font applications.
www.OnBarcode.com
Listing 2-2. The app/controllers/salutation_controller.rb File
Quick Response Code Generation In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
Paint QR Code In VS .NET
Using Barcode encoder for Reporting Service Control to generate, create Denso QR Bar Code image in Reporting Service applications.
www.OnBarcode.com
class SalutationController < ApplicationController def hello @message = 'Hello World!' end end
Print Barcode In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Encode QR Code 2d Barcode In None
Using Barcode generator for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
Actions are implemented as Ruby methods. You can always tell a method definition because of the def keyword. Inside the action, we re setting a Ruby instance variable called @message, the value of which we ll output to the browser.
PDF-417 2d Barcode Printer In None
Using Barcode drawer for Word Control to generate, create PDF417 image in Word applications.
www.OnBarcode.com
USS Code 39 Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Creating a Template
Code 39 Extended Printer In None
Using Barcode encoder for Software Control to generate, create Code-39 image in Software applications.
www.OnBarcode.com
Create Code-39 In .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code 3/9 image in Visual Studio .NET applications.
www.OnBarcode.com
With our action successfully defined, our next move is to add some HTML into the mix. Rails makes it easy by separating the files that contain HTML into their own directory as per the MVC pattern. In case you haven t guessed, HTML is the responsibility of the view. If you look in the app/views directory, you ll see another product of the controller generator: a directory called salutation. It s linked to the salutation controller, and it s where you put template files that correspond to your salutation actions.
Painting Barcode In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Drawer In .NET Framework
Using Barcode generator for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
CHAPTER 2 GETTING STARTED
Barcode Encoder In None
Using Barcode creation for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com
Barcode Recognizer In Visual Basic .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Note Because Rails allows you to embed Ruby code in your HTML, you use the .rhtml (Ruby + HTML)
extension for your templates.
The default way to render a template in response to a request for an action is remarkably simple: just name it the same as the action. Since we want to show a response to the hello action, we ll name our file hello.rhtml, and Rails will render it automatically. This is quite easy to grasp in practice. Take a look at Figure 2-7 for a visual cue as to how controllers and templates correspond.
Figure 2-7. Controllers correspond to a directory in app/views
Start by creating a new, blank file in app/views/salutation/. Name it hello.rhtml and add the code shown in Listing 2-3. Notice the <%= %> syntax that surrounds the @message variable. These are known as ERb (Embedded Ruby) output tags. We ll talk more about ERb in 6. For now, it s only important that you know that whenever you see <%= %> in a template, whatever is between the tags will be evaluated as Ruby and the result will be printed out.
CHAPTER 2 GETTING STARTED
Listing 2-3. The app/views/salutation/hello.rhtml File
<html> <body> <h1><%= @message %></h1> </body> </html>
It looks like we re all set. The salutation controller will field the request for hello and automatically render the hello.rhtml template. Start up the web server again using the script/server command and request the URL http://localhost:3000/salutation/hello in your browser. You should see the result shown in Figure 2-8.
Figure 2-8. The Hello World application
Sure enough, there s our greeting! The hello template read the @message variable that we set in the controller, and with a little help from ERb, printed it out to the screen.
CHAPTER 2 GETTING STARTED
Now, we should mention something here in case you didn t notice. The URL http://localhost:3000/salutation/hello maps directly to the controller and action we created. This is no mere coincidence. It s just another example of convention over configuration, most succinctly expressed by the default URL pattern:
:controller/:action/:id
We re not sending anything along in the :id placeholder, but if we were, it would be available to us as a parameter by the same name (params[:id]). We ll talk about parameters more in 6, but for now, it s enough that you understand the default URL format so you can make the mental connection between URL segments and the code in your application. When you read the URL http://localhost:3000/salutation/hello, you can translate it into a request for the hello action on the salutation controller.
Summary
We covered a lot in this chapter, so you should be proud of yourself. We went from not having Rails installed to getting a basic Rails application up and running. You learned how to install Ruby and how to manage packages with RubyGems (which we used to install Rails). You also learned how to create a new Rails project using the rails command, and how to use the generator to create a new controller. And you learned how controller actions correspond to templates. The stage is now set for the next chapter, where we ll begin building a more full-featured project.
Copyright © OnBarcode.com . All rights reserved.