Ruby on Rails Study Notes (1)

Source: Internet
Author: User
1 Check whether the entered content is empty. In Models
Validates_presence_of: title,: Description,: image_url
2 Verify that the entered content is Numeric
Validates_numericality_of: Price
3 Verify that the price of the input data is no less than 0.01
Protected
Def validate
Errors. Add (: price, "shocould be at least 0.01") if price. nil? | Price <0.01
End
4 Verify a field. duplicate fields are not allowed in the database.
Validates_uniqueness_of: Title
5 Verify image URL Connection
Validates_format_of: image_url,
: With => % R {\. (GIF | JPG | PNG) $} I,
: Message => "must be a URL for a GIF, JPG, PNG Image"
6 Adjust a style sheet
Find the row of the style table in the HTML. ERB file of the APP/views/layouts directory and change it
<% = Stylesheet_link_tag 'scaffol', 'dest' %>
7 A DataGrid
<% For product in @ products %>
<Tr valign = "TOP" class = "<% = Cycle ('LIST-line-odd', 'LIST-line-even') %>"> Line Style Alternation
<TD>
</TD>
& Lt; TD width = "60%" & gt;
<SPAN class = "list-title"> <% = H (product. Title) %> </span> <br/> H () converts HTML, 2.0 seems to be useless
<% = H (truncate (product. Description, 80) %> Truncate () truncate the string. 2.0 does not seem to be 80.
</TD>
<TD class = "list-Actions">
<% = Link_to 'show ',: Action => 'show',: Id => product %> <br/>
<% = Link_to 'edit',: Action => 'edit',: Id => product %> <br/>
<% = Link_to 'deststroy', {: Action => 'deststroy',: Id => product },
: Confirm => "Are you sure? ", Added. Confirm deletion dialog box
: Method =>: Post %> After deletion, You need to refresh the page
</TD>
</Tr>
<% End %>
8 Add a parameter to the Controller.
Def Index When a controller is called, if no specific action is specified, index is used by default.
@ Products = product. find_products_find_for_sale This parameter automatically finds the find_products_find_for_sale method of models and obtains the value.
End
9 Declare a class method in Models
Def self. find_product_for_sale Self. Declares class methods, which can be called by using product., without limiting the Controller
Find (: All,: Order => "title ")
End
10 The HTML. ERB file in the app/views/layouts directory is the CSS file used to change the page.
The. rhtml in the app/views/layouts directory is a template file and provides a layout template.
11 Add a template file rhtml
<Div id = "banner">
<% = Image_tag ("logo.png") %>
<% = @ Page_title | "pragmatic bookshelf" %> Specify the content to be displayed in the header
</Div>
<Div id = "columns">
<Div id = "side">
<A href = "http: // www..."> Home </a> <br/>
<A href = "http: // www.../faq"> Questions </a> <br/>
<A href = "http: // www.../News"> News </a> <br/>
<A href = "http: // www.../contact"> contact </a> <br/>
</Div>
<Div id = "Main">
<% = Yield: Layout %> The actual content of the page is displayed at this location
</Div>
</Div>
12 International Currency output
<SPAN class = "price"> <% = number_to_currency (product. Price) %> </span> Number_to_currency ()
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.