Hello merb
merb هى webframework خاصة بروبى مثل رايلز لكن ايه الهدف ؟
merb vs rails1- التستيب فى اكتر من طريقة ولكن المفضلة هى القياسية
$ sudo gem install merb --include-dependencies
ملحوظة: ممكن تواجه مشاكل فى التستيب على Ubuntu/Ubuntu-based
فقم بتستيب ruby-full وممكن تواجه مشكله فى حال عدم وجود ال sqlite.h لذا قم بتستيب libsqlite3-dev
ايضا قم بتستيب mongrel
$ sudo apt-get install ruby-full
$ sudo apt-get install libsqlite3-dev
$ sudo gem install mongrel
2- عمل basic application
$ merb-gen app my_application
$ cd my_application
$ merb
merb-gen هو ال generator الخاص ب merb و app بتعبر عن انه ينشئ ابلكيشن بإسم my_application
هيتم انشاء فولدر بإسم my_application انقل اليه بإستخدام cd وشغل السرفر بإستخدام merb
3- Hello World
striky@striky-desktop:~/tuts/merbtut$ merb-gen app helloworld
Generating with app generator:
[ADDED] Rakefile
[ADDED] app/helpers/global_helpers.rb
[ADDED] app/views/exceptions/not_found.html.erb
[ADDED] app/views/exceptions/not_acceptable.html.erb
[ADDED] app/views/exceptions/internal_server_error.html.erb
[ADDED] app/models/user.rb
[ADDED] app/controllers/application.rb
[ADDED] app/controllers/exceptions.rb
[ADDED] autotest/merb_rspec.rb
[ADDED] autotest/discover.rb
[ADDED] autotest/merb.rb
[ADDED] config/dependencies.rb
[ADDED] config/database.yml
[ADDED] config/router.rb
[ADDED] config/init.rb
[ADDED] config/environments/development.rb
[ADDED] config/environments/test.rb
[ADDED] config/environments/rake.rb
[ADDED] config/environments/production.rb
[ADDED] config/environments/staging.rb
[ADDED] config/rack.rb
[ADDED] public/stylesheets/master.css
[ADDED] public/javascripts/application.js
[ADDED] merb/session/session.rb
[ADDED] merb/merb-auth/setup.rb
[ADDED] merb/merb-auth/strategies.rb
[ADDED] spec
[ADDED] gems
[ADDED] tasks/merb.thor
[ADDED] .gitignore
[ADDED] public/.htaccess
[ADDED] tasks/doc.thor
[ADDED] public/javascripts/jquery.js
[ADDED] doc/rdoc/generators/merb_generator.rb
[ADDED] doc/rdoc/generators/template/merb/merb.css
[ADDED] doc/rdoc/generators/template/merb/merb_doc_styles.css
[ADDED] doc/rdoc/generators/template/merb/index.html.erb
[ADDED] doc/rdoc/generators/template/merb/prototype.js
[ADDED] doc/rdoc/generators/template/merb/merb.rb
[ADDED] doc/rdoc/generators/template/merb/api_grease.js
[ADDED] public/robots.txt
[ADDED] public/favicon.ico
[ADDED] public/images/merb.jpg
[ADDED] public/merb.fcgi
[ADDED] app/views/layout/application.html.er
b
كدا انشئنا التطبيق الرهيب بتاعنا
نشوف الهيكلية الخاصة بيه
striky@striky-desktop:~/tuts/merbtut$ mytree helloworld/ -f
| public [DIR]
|+ favicon.ico
|+ .htaccess
|+ merb.fcgi
|+ robots.txt
| stylesheets [DIR]
|+ master.css
| images [DIR]
|+ merb.jpg
| javascripts [DIR]
|+ jquery.js
|+ application.js
| log [DIR]
| config [DIR]
|+ init.rb
|+ rack.rb
| environments [DIR]
|+ test.rb
|+ production.rb
|+ development.rb
|+ staging.rb
|+ rake.rb
|+ database.yml
|+ dependencies.rb
|+ router.rb
| doc [DIR]
| rdoc [DIR]
| generators [DIR]
|+ merb_generator.rb
| template [DIR]
| merb [DIR]
|+ prototype.js
|+ merb.css
|+ merb.rb
|+ api_grease.js
|+ merb_doc_styles.css
|+ index.html.erb
| app [DIR]
| helpers [DIR]
|+ global_helpers.rb
| models [DIR]
|+ user.rb
| views [DIR]
| layout [DIR]
|+ application.html.erb
| exceptions [DIR]
|+ not_found.html.erb
|+ not_acceptable.html.erb
|+ internal_server_error.html.erb
| controllers [DIR]
|+ application.rb
|+ exceptions.rb
|+ Rakefile
| tasks [DIR]
|+ merb.thor
|+ doc.thor
| spec [DIR]
|+ spec_helper.rb
|+ spec.opts
| gems [DIR]
| autotest [DIR]
|+ merb.rb
|+ merb_rspec.rb
|+ discover.rb
|+ .gitignore
| merb [DIR]
| merb-auth [DIR]
|+ strategies.rb
|+ setup.rb
| session [DIR]
|+ session.rb
واضحة الهيكلية اهم مافيها فولدر ال app وفيه بتعيش الcontrollers/models/views وال helpers
فولدر الconfig فيه ملفات للإعدادات اهمها database.yml (دا فى حال لو هتستخدم database)
والباقيين واضحين
rounter.rb للتحكم فى ال urls
شغل السرفر بإستخدام merb
**المفروض يظهرلك شئ مشابه للتالى
striky@striky-desktop:~/tuts/merbtut/helloworld$ merb
Loading init file from /home/striky/tuts/merbtut/helloworld/config/init.rb
Loading /home/striky/tuts/merbtut/helloworld/config/environments/development.rb
~ Connecting to database...
~ Loaded slice 'MerbAuthSlicePassword' ...
~ Parent pid: 11535
~ Compiling routes...
~ Activating slice 'MerbAuthSlicePassword' ...
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000

على الشمال فى Exception لأننا مش حددنا / هتروح لفين هنعدلها حالا
ننشئ كنترولر بإسم greet وفيه actions مثلا hi, bye
striky@striky-desktop:~/tuts/merbtut/helloworld$ merb-gen controller greet
Loading init file from /home/striky/tuts/merbtut/helloworld/config/init.rb
Loading /home/striky/tuts/merbtut/helloworld/config/environments/development.rb
Generating with controller generator:
Loading init file from /home/striky/tuts/merbtut/helloworld/config/init.rb
Loading /home/striky/tuts/merbtut/helloworld/config/environments/development.rb
[ADDED] app/controllers/greet.rb
[ADDED] app/views/greet/index.html.erb
[ADDED] spec/controllers/greet_spec.rb
[ADDED] app/helpers/greet_helper.rb
افتح ملف greet.rb هتلقيه مشابه لكدا
class Greet < Application
# ...and remember, everything returned from an action
# goes to the client...
def index
render
end
end
تمام ننشئ actions جديدة عدل الملف ليصبح كالتالى
class Greet < Application
# ...and remember, everything returned from an action
# goes to the client...
def index
render
end
def hi
render #should use display instead.
end
def bye
render #should use display instead.
end
end
هنا render هتعمل load ل view بإسم views/greet/hi.html.erb
views:
hi.html.erb
<center><h1>Hello, merb!</h1></center>

عند استدعاء المسار التالى greet/hi هيتم استدعاء ال view hi.html.erb
bye.html.erb
<center><h1>I don't want to go!</h1></center>
عند استدعاء المسار greet/bye هيتم عمل لود للview bye.html.erb

لتعديل الصفحة الرئيسية فى ملف ال router.rb عدل السطر اللى بيبدأ ب match('/') .to للتالى
match('/').to(:controller => 'greet', :action =>'index')
او استبدله بصفحتك الرئيسية
مصادر
http://www.merbivore.com/http://4ninjas.org/merb/