[Ror] Ruby on Rails

Source: Internet
Author: User
Tags ruby on rails
View

View is divided into builder mode and rhtml mode.

The following is an example of the builder mode.

XML. Div (: class => "productlist") Do
XML. timestamp (time. Now)
@ Products. Each do | product |
XML. Product do
XML. productname (product. Title)
XML. Price (product. Price,: currency => "USD ")
End
End
End

If the tag and built-in method are repeated, you can use something! () To generate tags

Rhtml is similar to JSP. <% =... %> the output <%... %> flat <%...-%> automatically deletes its own empty rows.

The H method provides HTML Encoding
Helper

Xxxcontroller corresponds to a xxxhelper (xxx_helper.rb) to write the help methods, which are visible in rhtml.

To implement cross-controller calls, you can

* Write the method in application_helper.rb
* Import data through helper: some_helper.rb in the Controller
* Import via helper somehelper in controller (when the class has been planted)

The helper_method can also change the method in controller to helper. However, this is not recommended and the encapsulation is broken.

Rails has a lot of built-in helper available, such as the conversion of various units to helper (especially meaningful to view). For details, refer to rdoc's special helper

* Debug prints the object in HTML-visible mode to facilitate debugging.
* Simeple_format: simply format the string and add

Section and
* Excerpt adds... "... some words fo..." to both sides of the character ..."
* Add highlight (string, highword...
* Truncate (sring, maxlen) add... "Hello this is my..." to the right of the character ..."
* Pluralize (Num, word) automatically adds the plural form 1 person-> 2 people
* MarkdownArticleFilter
* Texttile Article Filter

Process Link

Link_to "XXXX", {hash}, {hash}

The first is the link text, the second hash is the same as url_for, and the third hash is the tag added by the user in <A>, such

<% = Link_to "delete", {: controller => "admin ",
: Action => "delete ",
: Id => @ Product
},
{: Class => "redlink ",
: Confirm => "Are you sure? "
}
%>

Like link_to, The button_to parameter generates a button to jump (secure post protocol), but button_to must be in a form separately. HTML does not allow nested form.

Link_to_unless_current automatically identifies the current link. If the current URL is the same as the link, only text is generated.

Image_tag nested into link_to

Mail_to has one: encode => "JavaScript", which can implement email address encoding to prevent spider attacks.

Stylesheet_link_tag add CSS

Auto_descovery_link_tag automatically declares RSS

By default, all images are stored in/images CSS in/stylesheets. Unless/XXX is used to specify the root path, you can modify actioncontroller: base. asset_host = "http://media.my.url/assets" # common prefix so that static content can be achieved specifically in one
Paging

The paginator object in the controller involves two parts: controller and view. The paginator object is used for paging. The default page size is 10 pages.

@ User_pages, @ users = paginate (: users,: order_by => 'name ')

When <% = pagination_links (@ user_pages) %> is used in the page, the page Link is automatically generated.

For more information, see rdoc.
Form Processing

In essence, rails simply sets upstream parameters to a hierarchical Hash (AAA. bbb. CCC ing is AAA [BBB] [CCC]), which automatically exchanges data with activerecord, but can also be directly read.

Param [: XX] [: yy] rails is recommended to read with symbol (string can also be used)

The first two parameters of most input helper are variable and: attribute. The last one is the added HTML options. The example <% = xxx_field: Variable,: attribute, options_hash %>

Note: update_attribute automatically calls the Save method.
Method In formhelper for form processing (bound to Model)
Normal input box

* Text_field
* Hidden_field
* Password_field
*

Text_area
*

The third parameter of radio_button is tag_value. When it is equal to the value, it is displayed as selected.
* The check_box value must be true/false, or the last two parameters converted to the int value (not 0 is true) are on_value and off_value, which are the values submitted when the check_box value is enabled or disabled, the default value is "1" and "0"

List

Select (: Variable,: attribute, choices, options, html_options)

The choice parameter is enumerable. when the length of the enumerable element is an array of 2 such as [a, B], the first element of the array is value, and the second element is key.

Collection_select (: Variable,: attribute, choices,: inner_attr_for_key,: inner_attr_for_value) is used to list sets.
Group list

Do not read @@@
Date input box

Date_select (: Variable,: attribute, options) datetime_select (...) is the date for processing model-related data.

Select_xxx is a normal date (accessed directly through Params)

Why is it useful?
Upload files

In form, you need to add: multipart => true. In essence, you can get the data (A stringio) through the corresponding parameters and read the full content to the string, which is equivalent to getting the file content for processing.
Error Handling

The on-site helper parameter will automatically read the error message for field (errors. on (field). When an error is found, a class = fieldwitherrors div is automatically generated. You can control the display through CSS. to directly read the error information of a field, you can use <% = error_message_on (: bean,: Field) %> to read all errors using <% = error_messages_for (: bean) %>
Formtaghelper and model-independent methods

The methods in formtaghelper have more than one _ tag end in form.

* Start_form_tag and end_form_tag form start and end
* Submit_tag submit button

The help methods in other formhelper have a corresponding _ Tag version. The parameter acceptance is: name, value, html_options.

Example:

<% = Text_field_tag (: arg1, @ Params [: ARGs],: size => 3) %>
Layout technology (sitemesh template)

In the layout template, you can access all objects that can be accessed on the corresponding page, and there are also the @ content_for_layout parameter to point to the output content of the entire page.

Automatic Search: A xxx_controller corresponds to a XXX. rhtml/rxml file with the same name in layout as its layout,

Explicit statement:

1. Use layout to declare a string in the controller. The following parameters are supported: parameter t and: only (filter: Action name)
2. If it is declared as nil, layout is disabled.
3. Declare it as a: symbol to get the string name through the current symbol method

String corresponds to string. rhtml or string. rxml

Render: directly call render (: Layout => "layouts/xxxx"). You can use layout or use: Layout => false to disable layout.
Part of the partial page template page

The page of a page segment must start with _. It is placed in the same path as controller and accessed using render (: Partial => "XXX",: Object => @ newinst ).

The file name must be a ruby variable specification or a common file name specification. Parameters injected by an object are accessed by a file name, for example, _ myname. rhtml gets the myname parameter. to append other parameters, use: locals = {...}

If ignore: the object parameter indicates that @ xxx is automatically passed into the xxx variable in _ XXX. rhtml, that is, when the variable names in @ instance and partial are the same, the object parameter can be omitted:
Use the partial of the Set

You can use render (: Partial => "XXX",: Collection => XXX) to specify a set. The elements in the set are transmitted to partial content as the primary element, at the same time, a xxx_counter will be added as the index. In this case, spacer_template => "yyy" can be used to specify the blank template used between two elements.

If the name specified by "partial =>" xxx/yyy "contains/, rails starts from/APP/views by default, this method allows multiple controllers to use a partial conetent
Components component

Use render_component (: controller => 'xxx',: Action => 'fdas') through render_component on the page ')

Pay attention to avoid circular references, so it is best to use render (: Layout => false...) for component actions ...) or declare layout "XXX", "release T =>: xxxx in the class.
Independent Component Components

* Must be under components/xxx
* The XXX: mycontroller statement must be in model XXX.
* Declare uses_component_template_root at the beginning of the class
* Usage: <% = render_component): controller => 'xxx/my',: Action => 'yyy') %>

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.