<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    子在川上曰

      逝者如斯夫不舍晝夜
    隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
    數據加載中……

    Rails學習筆記(5)第6、7、8章摘要

    創建一個項目:rails depot

    執行SQL腳本:mysql depot_development < db/create.sql
    創建三個庫,分別用于開發、測試、生產:depot_development、depot_test、depot_production。
    每個表都應該帶一個和業務無關的ID字段。
    在rails配置數據庫的連接:config\database.yml。密碼之前冒號之后要間隔一個空格。

    ruby script/generate scaffold Product Admin,針對Product表產生一整套的網頁程序(增刪改),Admin是控制器名
    ruby script/generate controller Store index   創建一個名為Store的控制器,并含有一個index() Action方法。


    啟動WEB服務:ruby script/server。(http://localhost:3000/admin


    模型類:
    。validates_presence_of  :title, :desc, :image_url   必須不為空
    。validates_numericality_of :price  必須是數字
    。validates_uniqueness_of   :title   必須唯一
    。validates_format_of       :image_url,
                                :with    => %r{^http:.+\.(gif|jpg|png)$}i,
                                :message => "must be a URL for a GIF, JPG, or PNG image"  文件名必須是圖片文件的擴展名
    。模型保存到數據庫之前會調用validate方法,例:
      def validate
        errors.add(:price, 
    "should be positive") unless price.nil? || price > 0.0
      end



    ActiveRecord的方法屬性:
    。content_columns 得到所有字段。content_columns.name字段名
    。日期在今天之前,按日期倒序排列。self表示它是一個類方法(靜態方法)。
      def self.salable_items
        find(:all, :conditions 
    => "date_available <= now()", :order => "date_available desc")
      end


    以下為鏈接,效果相當于以前的“http://..../show.jsp?id=11
          <%= link_to 'Show', :action => 'show', :id => product %><br/>
          
    <%= link_to 'Edit', :action => 'edit', :id => product %><br/>
          
    <%= link_to 'Destroy', { :action => 'destroy', :id => product }, :confirm => "Are you sure?" %>


    <%=  if @product_pages.current.previous 
           link_to(
    "Previous page", { :page => @product_pages.current.previous })
         end
    %>

    <%= if @product_pages.current.next 
          link_to(
    "Next page", { :page => @product_pages.current.next })
        end
    %>


    truncate(product.desciption, 80)  顯示產品描述字段的摘要(80個字符)
    product.date_available.strftime("%y-%m-%d") 日期字段的格式化顯示
    sprintf("%0.2f", product.price) 數值的格式化顯示, number_to_currency方法也有類似功能

    public/stylesheets 目錄保存了網頁所用的CSS式樣表。用ruby script/generate scaffold ....生成框架代碼的網頁自動使用scaffold.css


    布局模板
    。app/views/layouts目錄中創建一個與控制器同名的模板文件store.rhtml,則控制器下所有網頁都會使用此模板

     

    <html>
        
    <head>
          
    <title>Pragprog Books Online Store</title>
          
    <%= stylesheet_link_tag "scaffold""depot", :media => "all" %>
        
    </head>
        
    <body>
            
    <div id="banner">
                
    <img src="/images/logo.png"/>
                
    <%= @page_title || "Pragmatic Bookshelf" %>
            
    </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">
                    
    <% if @flash[:notice] -%>
                      
    <div id="notice"><%= @flash[:notice] %></div>
                    
    <% end -%>
                    
    <%= @content_for_layout %>
                
    </div>
            
    </div>
        
    </body>
    </html>

    。<%= stylesheet_link_tag "scaffold""depot", :media => "all" %>生成指向scaffold.css、depot.css兩個式樣表的<link>標簽
    @page_title 各個頁面標題變量
    @flash[:notice] 提示性的信息(key=notice),這是一個公共變量。
    。<%= @content_for_layout %> 位置嵌入顯示控制器內名網頁。由于其他網頁變成了模板的一部份,所以其<html>等標簽應該去掉。



    store_controller.rb ,當從session里取出某購物車不存在,則新建一個,并存入session。最后把此購物車賦給變量cart。

      def find_cart
        @cart 
    = (session[:cart] ||= Cart.new)
      end


    ruby  script/generate  model  LineItem,創建一個LineItem的數據模型,附帶還會創建相應的測試程序。

    store_controller.rb:

      def add_to_cart
        product 
    = Product.find(params[:id])
        @cart.add_product(product)
        redirect_to(:action 
    => 'display_cart')
      rescue
        logger.error(
    "Attempt to access invalid product #{params[:id]}")
        redirect_to_index(
    'Invalid product')
      end

    。params是URL的參數數組
    。redirect_to轉向語句
    。rescue 異常,相當于Java的Exception
    。redirect_to_index是application.rb中的一個方法,這個文件里的方法是各控制器公開的。
    。logger是rails內置的變量。

    class Cart

      attr_reader :items
      attr_reader :total_price
      
      def initialize
        empty
    !
      end

      def add_product(product)
        item 
    = @items.find {|i| i.product_id == product.id}
        
    if item
          item.quantity 
    += 1
        
    else
          item 
    = LineItem.for_product(product)
          class Cart

      # An array of LineItem objects
      attr_reader :items

      # The total price of everything added to this cart
      attr_reader :total_price
     
      # Create a new shopping cart. Delegates this work to #empty!
      def initialize
        empty!
      end

      # Add a product to our list of items. If an item already
      # exists for that product, increase the quantity
      # for that item rather than adding a new item.
      def add_product(product)
        item = @items.find {|i| i.product_id == product.id}
        if item
          item.quantity += 1
        else
          item = LineItem.for_product(product)
          @items << item
        end
        @total_price += product.price
      end

      # Empty the cart by resetting the list of items
      # and zeroing the current total price.
      def empty!
        @items = []
        @total_price = 0.0
      end
    end

        end
        @total_price 
    += product.price
      end

      def empty
    !
        @items 
    = []
        @total_price 
    = 0.0
      end
    end 


    。由于Cart沒有對應的數據表,它只是一個普通的數據類,因此要定義相應的屬性。
    。@items << item 把對象item加入到@items數組
    。@items=[]   空數組


    如果出現SessionRestoreError錯誤,則檢查application.rb是否做了如下的模型聲明。因為對于動態語句,session把序列化的類取出時,否則是無法知道對象類型的,除非聲明一下。

    class ApplicationController < ActionController::Base
      model :cart
      model :line_item
    end


    <%  ... -%>  -%>相當于一個換行符


    @items.find(|i| i.product_id == product.id)  |i|的作用應該相當于@items數組中的一個元素(書上沒有提到,要查一下ruby語法)。


    if @items.empty?   判斷數組是否為空數組

    flash[:notice] flash可以在同一個Session的下一個請求中使用,隨后這些內容就會被自動刪除(下一個的下一個就被清空了??)。
    實例變量是代替不了flash的,因為IE無狀態的特性,在下一個請求中,上一個請求的實例變量已失效。


    當異常沒有被任何程序捕捉,最后總會轉到ApplicationController的rescue_action_in_public()方法


    各視圖可以使用appp/helpers目錄下的各自相應的輔助程序中提供的方法。

     

    posted on 2007-04-10 09:47 陳剛 閱讀(1690) 評論(1)  編輯  收藏 所屬分類: Rails&Ruby

    評論

    # re: Rails學習筆記(5)第6、7、8章摘要  回復  更多評論   

    ruby script/generate scaffold Product Admin
    貌似 在rails 2.0中,不支持這種方法了吧 貌似不能指定建立控制器.

    現在的建 scaffold的方法
    ruby script/generate scaffold Product title:string .....

    不知道我理解的對否,望指正
    2008-01-18 15:08 | 就這調調
    主站蜘蛛池模板: 最近中文字幕2019高清免费| 久久精品成人免费观看97| 1000部无遮挡拍拍拍免费视频观看 | 国产免费久久精品丫丫| 亚洲av高清在线观看一区二区| 亚洲精品人成网线在线播放va| 成在人线AV无码免费| 亚洲色大成网站www| 日本免费无遮挡吸乳视频电影| 亚洲人成网站在线在线观看 | 国产a不卡片精品免费观看| 国产亚洲一卡2卡3卡4卡新区| 日本免费一区尤物| 狠狠热精品免费观看| 国产国拍亚洲精品福利 | 亚洲一欧洲中文字幕在线| 免费精品国产自产拍在线观看图片| 亚洲午夜在线播放| 成人永久免费福利视频网站| 美女视频黄频a免费| 亚洲国产另类久久久精品小说| 日韩精品无码一区二区三区免费 | 免费在线黄色网址| 久久精品成人免费国产片小草| 无码专区—VA亚洲V天堂| 日韩免费一区二区三区在线| 亚洲AV色欲色欲WWW| 亚洲综合色在线观看亚洲| 日韩精品内射视频免费观看 | jizz日本免费| 911精品国产亚洲日本美国韩国 | 亚洲精品GV天堂无码男同| 国产成人亚洲精品影院| 久久精品免费一区二区| 青青免费在线视频| 亚洲情a成黄在线观看动漫尤物| 好男人视频社区精品免费| 久久久久久噜噜精品免费直播| 亚洲国产成a人v在线| 波多野结衣亚洲一级| 免费永久国产在线视频|