some Rails leanrning:Rails Ajax,Validates,Cycle

本文介绍了如何在Rails应用中使用Ajax实现动态更新内容,包括使用link_to_remote进行页面元素更新、利用form_remote_tag发送表单数据及通过observe_field监听字段变化以发起Ajax请求等方法。

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 should 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

转载于:https://www.cnblogs.com/caca/archive/2006/06/20/430018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值