锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
routes.rb涓繖鏍峰畾涔?
admin.resources :articles
admin.resources :tags
end
闇瑕佸articles,tags鎻愪緵涓涓粺涓鐨勫悗鍙扮鐞嗙晫闈紝鏍規嵁rails涓殑layout鍔熻兘錛岀寽鎯蟲槸鍚︿細鏈塴ayouts/admin.html.erb榪欐牱涓涓拡瀵筺amespace鐨勬ā鏉塊〉錛屼絾灝濊瘯鍚庡彂鐜版牴鎹笉琛岋紝涓嬮潰鏄痝oogle鍒扮殑涓涓棶棰橈細
file? For instance, the UsersController will use layouts/users.rhtml
if it exists. Is there something similar for Admin::UsersController
or will it default to the same file? I tried layouts/
admin_users.rhtml and layouts/admin/users.rhtml with no luck.
浠栦篃灝濊瘯浜嗗悓鏍風殑闂錛?span style="color: #000000;">layouts/users.rhtml, layouts/admin_users.rhtml, layouts/admin/users.rhtml錛岄兘鏄痭o luck.
榪欓噷鍙互浣跨敤涓涓彇宸х殑鏂規硶錛屽湪controller涓彲浠ラ氳繃layout 鏂規硶鏉ユ寚瀹氫嬌鐢ㄥ摢涓ā鏉塊〉錛岃繖閲屽彲浠ュ啓涓涓狟aseController:
layout 'admin'
end
鍦˙aseController涓寚瀹氭ā鏉塊〉涓篴dmin.html.erb
鐒跺悗灝哸rticles鍜宼ags鐨凜ontroller閮芥敼涓虹戶鎵胯嚜BaseController銆?
]]>
script/generate scaffold article title:string body:text
鍦╮outes.rb涓坊鍔犲涓嬭礬鐢憋細
admin.resources :articles
end
鐢辨剰鍛崇潃緇檃rticle澧炲姞浜嗕竴涓悕涓篴dmin鐨刵amespace,灝哻ontrollers涓殑articles_controller.rb淇敼涓篶ontrollers/admin/articles_controller.rb錛屼篃灝辨槸灝嗚繖涓帶鍒跺櫒鏀懼埌admin榪欎釜鏂囦歡澶逛腑銆?br />
鍏堢湅涓涓嬬浉搴旂殑璺敱瑙勫垯,鎵цrake roures:
formatted_admin_articles GET /admin/articles.:format {:action=>"index", :controller=>"admin/articles"}
POST /admin/articles {:action=>"create", :controller=>"admin/articles"}
POST /admin/articles.:format {:action=>"create", :controller=>"admin/articles"}
new_admin_article GET /admin/articles/new {:action=>"new", :controller=>"admin/articles"}
formatted_new_admin_article GET /admin/articles/new.:format {:action=>"new", :controller=>"admin/articles"}
edit_admin_article GET /admin/articles/:id/edit {:action=>"edit", :controller=>"admin/articles"}
formatted_edit_admin_article GET /admin/articles/:id/edit.:format {:action=>"edit", :controller=>"admin/articles"}
admin_article GET /admin/articles/:id {:action=>"show", :controller=>"admin/articles"}
formatted_admin_article GET /admin/articles/:id.:format {:action=>"show", :controller=>"admin/articles"}
PUT /admin/articles/:id {:action=>"update", :controller=>"admin/articles"}
PUT /admin/articles/:id.:format {:action=>"update", :controller=>"admin/articles"}
DELETE /admin/articles/:id {:action=>"destroy", :controller=>"admin/articles"}
DELETE /admin/articles/:id.:format {:action=>"destroy", :controller=>"admin/articles"}
/:controller/:action/:id
/:controller/:action/:id.:format
瀵規瘮涓涓嬩笉鍔燼dmin榪欎釜namespace鏃剁殑璺敱錛?br />
formatted_article_index GET /article.:format {:action=>"index", :controller=>"article"}
POST /article {:action=>"create", :controller=>"article"}
POST /article.:format {:action=>"create", :controller=>"article"}
new_article GET /article/new {:action=>"new", :controller=>"article"}
formatted_new_article GET /article/new.:format {:action=>"new", :controller=>"article"}
edit_article GET /article/:id/edit {:action=>"edit", :controller=>"article"}
formatted_edit_article GET /article/:id/edit.:format {:action=>"edit", :controller=>"article"}
article GET /article/:id {:action=>"show", :controller=>"article"}
formatted_article GET /article/:id.:format {:action=>"show", :controller=>"article"}
PUT /article/:id {:action=>"update", :controller=>"article"}
PUT /article/:id.:format {:action=>"update", :controller=>"article"}
DELETE /article/:id {:action=>"destroy", :controller=>"article"}
DELETE /article/:id.:format {:action=>"destroy", :controller=>"article"}
/:controller/:action/:id
/:controller/:action/:id.:format
鎵鏈夌殑璺敱瑙勫垯涓婇潰錛岄兘鍔犱笂浜哸dmin榪欎釜namespace銆?br />
涓嬮潰灝辮灝唙iews鐩綍涓嬮潰鐨刟rticles鐩綍鏀瑰埌views/admin鐩綍涓嬮潰錛屽洜涓烘牴鎹礬鐢辮鍒欙紝浼氬埌views/admin/articles/鐩綍涓嬮潰鍘繪煡鎵劇浉搴旂殑妯℃澘銆?br />
鍙﹀錛岃繕闇瑕佸皢鍘熸潵鐨刵ew_article_url | new_article_path絳塰elper鏂規硶淇敼涓簄ew_admin_article_url | new_admin_article_path錛屽皢榪欎簺helper鏂規硶鍏ㄩ儴鍔犱笂admin榪欎釜namespace銆?br />
]]>
No problem! Override it with your new code, call super and…. Uh oh!! Suddenly this turned into a problem… Let me give some more context.
I was testing Ferret (and the acts_as_ferret plugin) in a project to provide full text search capabilities to our models. One of the things the plugin does is to add a new method to ActiveRecord, called find_with_ferret. That way, every model can use it. Great!
So I thought that would make sense for me to remove all diatrictics from the input text before letting ferret do its job. You know, like removing umlauts and all that.
I could do that by overriding this method with code to remove the undesired chars and then call its older version to finally do the search - something like calling super, but not quite. And I didn’t want my models to inherit from anything else than ActiveRecord::Base. That wouldn’t make any sense.
alias_method to the rescue!
You know that to redefine a method in an existing class you can open it up and rewrite it. But since you don’t wanna loose the behaviour provided by the original method, this is how you can achieve this:
And you’re good to go. On line 3 you’re just giving the original method an alias, making a copy of it.
Then you redefine it the way you like and on line 6 you call the old version to make sure u still got the same behaviour.
Now all my models can benefit of this change without requiring them to call another method nor inherit from another class.
Cool, huh?
#install Ruby and relative
apt-get install ruby
#install Rails and dependencies
gem install rails –include-dependencies -v=2.2.2
apt-get install sqlite3 swig libsqlite3-ruby libsqlite3-dev
apt-get install ruby1.8-dev libsqlite3-dev (required)
gem install sqlite3-ruby #compile this need libsqlite.h