Some rails leanrning: rails Ajax, validates, cycle

Source: Internet
Author: User

How to name the table, rails class
Your MySQL Database need a table named my_stories, note that: Not mystories, not my_story, and mapping class shocould named mystory.

Rails Ajax
It's very simple Using ajax on Rails Web application.
1. Using link_to_remote
<Head>
<% = Javascript_include_tag "prototype" %>
</Head>
<% = Link_to_remote ("Click here ",
: Update => "time_div ",
: URL =>{: action =>: say_when },
: Position => "after") %>
<Div id = "time_div">
2. Using form_remote_tag
<Head>
<% = Javascript_include_tag "prototype" %>
</Head>
<% = Form_remote_tag (: update => "my_list ",
: URL =>{: action =>: add_item },
: Position => "TOP") %>
New item text:
<% = Text_field_tag: newitem %>
<% = Submit_tag "add item with Ajax" %>
<% = End_form_tag %>
<Ul id = "my_list"> </ul>
3. Using observers
Make an Ajax call to an Action Handler whenever the value of the field changes.
<Head>
<% = Javascript_include_tag "prototype" %>
</Head>
Live Search: <% = text_field_tag: searchtext %>
<% = Observe_field (: searchtext,
: Frequency => 0.25,
: Update =>: search_hits,
: With => "searchtext ",
: URL =>{: action =>: live_search}) %>
<P> Search Results: </P>
<Div id = "search_hits"> </div>

Validation
In model class just add like this
Validates_presence_of: title,: image_url
Validates_numericality_of: Price
Validates_uniquenss_of: Title
Validates_format_of: imageurl,
: With => % R {^ http:. + \. (GIF | JPG | PNG) $} I,
: Message => "must be a URL for a GIF, JPG, or PNG Image"

Cycle
For product in @ Products
End

@ Products. Each do | product |
End

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.