原创 rails Render和layout基礎學習

render作用 生成HTTP response 渲染和解釋子視圖(sub-view) render method in controllerin

原创 rails view簡單學習

view原理 ActionView: actionview gem ActionController : actionpack gem actionview和actionpack都可以單獨應用於任何ruby項目 view的查找 app/

原创 rails controller 簡單學習

Controller規則 app/controllers目錄 命名規則支持命名空間,以module的方式組織 Controller使用 class ApplicationController < ActionController::Ba

原创 rails 中will_paginate-bootstrap做分頁

思路:will_paginate是一個做分頁的gem,但是風格不太好看,使用will_paginate-bootstrap這個gem做一個帶有bootstrap樣式的分頁 Gemfile添加gem gem 'will_paginate-

原创 rails 管理員用戶管理簡單實現

路由:   namespace :admin do   root 'users#index'   resources :users do   collection do   get 'search'   end   end   end

原创 ruby 處理圖像

require 'mini_magick' img = MiniMagick::Image.open('/path/cartoon.jpg') 查看圖像的寬和長 w,h = img[:width],img[:height] 對圖像進

原创 rails routes路由筆記

routes : 規定了特定格式的URL請求到後端controller的action的分發規則 Name routes get 'users/:id', to: 'users#show', as: "user" RESTful

原创 mac的mysql意外無法正常啓動

sudo mysql.server start   Password:   Starting MySQL   .. ERROR! The serve

原创 rails 實現登錄和註冊功能

思路:用戶註冊需要user這個model, user控制器控制用戶的註冊功能,session控制器控制用戶的登錄功能,welcome控制器控制用戶登錄和退出的跳轉,爲了表單友好,增加bootstrap樣式 生成User model ra

原创 rails Model 多對多關係學習

介紹: model多對多關係,有兩種方法可以實現,第一種是has_and_belongs_to_many,第二種是has_many,下面分別分析一下 has_and_belongs_to_many 使用場景:一個a模型有多個b模型,b模

原创 ActiveRecord 的數據三種預加載形式 - includes, preload, eager_load和joins(不是預加載)

引言:在平常的ActiveRecord的開發中使用sql語句方式有很多,那麼ActiveRecord有沒有提供我們一些便利的方式來實現這個魔法,其實有的,想知道詳情請向下看。 當然在開始之前我們準備下數據: class Provinc

原创 ActiveSupport autoload源碼分析

在我們平常看一些gem或者開發中autoload用的還是很多的,閒的沒事總結下這個方法的內部實現,分析一下源碼。 在我們看源碼的時候經常看到如下代碼: module ActiveRecord extend ActiveSuppor

原创 ActionController::Base#render源碼解析

提出問題:爲什麼要研究這個? 在日常開發中controller中的render用的很多,或者說大部分用法都知道這麼用,但是我好奇這個render到底做了什麼,要不然用起來總感覺缺了點什麼,下面就來嘗試研究下源碼。 先前準備: welco

原创 instance_eval(instance_exec) 和 class_eval(class_exec)

instance_eval BasicObject.public_methods.grep /instance_eval/ => [:instance_eval] BasicObject.instance_methods.grep

原创 ActiveSupport eager_autoload源碼分析

eager_autoload加載方式在一些開發中很是常見,先舉個例子: module MyClass extend ActiveSupport::Autoload eager_autoload do autoload :