Dust Of Dream

          知識真的是一個圓么?

          Agile Web Development with Rails 3nd Edition 閱讀筆記

          Agile Web Development with Rails 3nd Edition 閱讀筆記
          1.session 使用
          1)如何使用數據庫保存session,以增加session的安全性?
            > rake db:sessions:create 創建Session相關的表結構
            > rake db:migrate 在數據庫中創建表
            修改 environment.rb 中使用database來代替默認采用基于cookie的存儲方式.
            # Use the database for sessions instead of the cookie-based default,
          # which shouldn't be used to store highly confidential information
          # (create the session table with 'rake db:sessions:create')
          config.action_controller.session_store = :active_record_store
          然后在基類application.rb中加入
          session :session_key => '_session_id' #(存入cookie的ID)
          # See ActionController::RequestForgeryProtection for details
          # Uncomment the :secret if you're not using the cookie session store
          protect_from_forgery :secret => '8c3e099237e6366fd2f5366e9c430e79' #(加密字符串,換個自己的吧)
          簡單一個session應用實例(找下session是否存在cart,不存在就創建一個新的扔進去)
          def find_cart
          unless session[:cart]
          session[:cart] = Cart.new 
          end
          session[:cart]
          end
          上面亂七八糟的代碼可以簡寫成 session[:cart] ||= Cart.new
          2.使用<%= render(:partial => "cart_item" , :collection => @cart.items) %>可以引入一個control,其中對應的control的文件名為"_cart_item.html.erb,在片段中引用的變量為"cart_item".在書中的實例如下:
          depot/app/views/store/add_to_cart.html.erb
          [CODE]
          <div class="cart-title">Your Cart</div>
          <table>
          <%= render(:partial => "cart_item" , :collection => @cart.items) %>
          <tr class="total-line">
          <td colspan="2">Total</td>
          <td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
          </tr>
          </table>
          <%= button_to "Empty cart" , :action => :empty_cart %>
          [CODE]
          [CODE]
          depot/app/views/store/_cart_item.html.erb
          <tr>
          <td><%= cart_item.quantity %>&times;</td>
          <td><%=h cart_item.title %></td>
          <td class="item-price"><%= number_to_currency(cart_item.price) %></td>
          </tr>
          [CODE]
          上面使用的是一個循環調用某個control,可以只傳入一個Object
          <%= render(:partial => "cart" , :object => @cart) %>
          3.如何使用Ajax
            先使用<%= javascript_include_tag :defaults %> 引入默認的Javascript
            然后使用
            <% form_remote_tag :url => { :action => :add_to_cart, :id => product } do %>
          <%= submit_tag "Add to Cart" %>
          <% end %>
          使用ajax觸發動作.
          一個簡單的Helper類里面定義方法的使用:
          def hidden_div_if(condition, attributes = {}, &block)
          if condition
          attributes["style" ] = "display: none"
          end
          content_tag("div" , attributes, &block)
          end
          在頁面上使用
          <% hidden_div_if(@cart.items.empty?, :id => "cart" ) do %>
          <%= render(:partial => "cart" , :object => @cart) %>
          <% end %>
          來引用.

          posted on 2008-12-04 18:45 Anemone 閱讀(339) 評論(0)  編輯  收藏 所屬分類: RUBY學習

          My Links

          Blog Stats

          News

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          新聞檔案

          相冊

          常去網站

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 芜湖县| 喀什市| 仙桃市| 汝州市| 嘉义市| 杭锦旗| 阿拉善右旗| 健康| 肃宁县| 寿阳县| 宽甸| 治县。| 云梦县| 交口县| 离岛区| 衡阳市| 甘德县| 罗定市| 托里县| 黄石市| 天长市| 巩留县| 武冈市| 荔波县| 定远县| 安陆市| 泸定县| 达日县| 昔阳县| 永济市| 射阳县| 西乌珠穆沁旗| 武胜县| 屯昌县| 祁连县| 织金县| 东安县| 长宁区| 满洲里市| 天全县| 新泰市|