zoukankan      html  css  js  c++  java
  • some Rails leanrning:Rails Ajax,Validates,Cycle

    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

  • 相关阅读:
    python中matplotlib所绘制的图包含了很多的对象
    pd.concat()命令
    Java设计模式——外观模式
    Java设计模式——桥接模式
    Java基础面试
    java web解决表单重复提交
    Servlet 工作原理解析
    Mysql日期函数
    解决多个下拉框动态级联初始化问题
    window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法
  • 原文地址:https://www.cnblogs.com/caca/p/430018.html
Copyright © 2011-2022 走看看