Learning: the basics of Ruby and helpers in the supplementary review

A. Ruby built-in helper methods

1. Open the file: app / views / layouts / application.html.erb (site layout presentation applications)

They focus on to ring up the line:

This line built with Rails stylesheet_link_tag method, introducing application.css all media types. For experienced Rails developers, this line of code looks very simple, but there are at least four point Ruby knowledge may make you confused: built-in Rails method without parentheses, symbols (Symbol) and bulk method call column (Hash).

2. Custom helper methods

Rails for us to use in the view in addition to providing many built-in method, but also allows us to define themselves. This method is called helper methods (helper)

Let's come to focus on this line of code:

This code requires that each view have to use custom header provide a method, for example:

Note: The master syntax with Python framework Django in similar, if not call provide a method in the view, that is not to provide part of the changes, then you will get the title will become:

Note: In other words, there is a common part of the title, but also shows the front of the vertical line.

To solve this problem, we want to customize a helper method called full_title. If the view is not defined in the title of the page, full_title common return header portion, i.e., "Ruby on Rails Tutorial Sample App"; if defined, it is followed by the variable portion of a vertical bar, as shown in the following code.

Open File: app / helpers / application_helper.rb (defined full_title assisted method)

Now, this helper method defined, we can use it to simplify the layout. The following line:

Read:

<title><%= full_title(yield(:title)) %></title>

In order to assist this method to work, we want to put unnecessary words "Home" to delete the home page of view, leaving only the public part of the title. First of all, we need to modify the test code, as shown in the following code, there are no confirmed title character string "Home".

Next, look at your own Guard test:

Note: then is to modify the bug myself:

Open the file: app / views / static_pages / home.html.erb (home page title not defined view)

The label file labeled statement note off (Ctrl + /: Quick Notes)

Then look at Gurad test:

Note: The test passed

II. Strings and methods

1. Console

We learn Ruby Rails is the main tool used by the console, which is used with the command-line tool Rails application interaction. Ruby console-based interactive program (irb) to develop, it is possible to use the full functionality of the Ruby language.

Rails Console $     # Open the Control Panel

Note: Both the console and there is no difference

2. Comment

# : The most commonly used on this one

3. String

https://www.cnblogs.com/rixian/p/11636750.html

4. Objects and messaging

Note:? Empty method have a question mark at the end, this is Ruby convention, a method returns a Boolean value that is true or false

If the branch words on the particular number of ELSIF ( the else + IF ) 
Boolean values may also be used && (and), || (or), and! (NOT) operator is used together 
everything is an object in Ruby, so is nil objects, so it can respond to a method

Note: Here tell us about nil? , Empty? , Blank? Three Brothers:

5. The method defined:

https://www.cnblogs.com/rixian/p/11634601.html

6. Review of secondary title

7. Class

Note: This needs to be said about the class, do not let the class name and the class name of a custom built-in method of the same class, or what to think of the consequences

(1). Define a class of their own, to make a clarification (create a file named example_user.rb in the application root directory)

Open the file: example_user.rb (User-defined class)

Note: Come on, we come to these things just write, make a note

①:attr_accessor :name, :email

We look at this line: the user's name and email address to create a property accessor access method (attribute accessor) this line of code, which is the definition of reading methods (getter) and setter (setter), for reading and set the instance variable @name and front @email had said. In Rails, instance variables meaning that they are automatically available in the view. Acting generally value instance variable is passed between different classes Ruby methods. Instance variables begin with the @ symbol, if not defined, the value is nil.

②:def initialize(attributes={})

The default value attributes parameter is an empty hash, so we can not define a user name or email address is not. (Recall from the previous basis, if the key does not exist will return nil, so if not defined: name key, attributes [: name] returns nil, attributes [: email]. The same is true)

③:def formatted_email

@name and instance variables are @email (as shown in the @ symbol), it is automatically available in formatted_email process.

(2). Quiz, quiz

Note: Good introductions to delete file rm -rf example_user.rb

。。。

First this, continue to wait

Guess you like

Origin www.cnblogs.com/rixian/p/11686824.html