Changeset 156
- Timestamp:
- 01/16/07 20:51:57 (2 years ago)
- Files:
-
- trunk/README (modified) (8 diffs)
- trunk/lib/merb.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/README
r135 r156 13 13 json or fjson 14 14 mime-types 15 archive-tar-minitar 16 rspec 15 17 16 18 Install these gems first then you can build the merb gem from svn trunk like so: 17 $ sudo gem install mongrel erubis json mime-types --include-dependencies18 $ svn co http://svn.devjavu.com/merb 19 $ sudo gem install mongrel erubis json mime-types archive-tar-minitar rspec --include-dependencies 20 $ svn co http://svn.devjavu.com/merb/trunk merb 19 21 $ cd merb 20 $ sudo rake install 21 22 **Important** 23 The new default filename extensions for templates are as follows 24 html -> .herb 25 js -> .jerb 26 xml -> .xerb 27 28 You can change the extensions to anything you want in your config file in 29 yourapp/dist/conf/merb.yml. See the default settings in the sample app. 30 31 First you need to install the dependencies. Merb requires the following gems 32 33 erubis 34 35 json 36 37 fjson # will be used if present. requires C extension. 38 39 # fastthread will be used if present and is reccommended. 40 sudo gem install fastthread --source=http://mongrel.rubyforge.org/releases/ 41 42 If you have checked out merb trunk from svn you will want to build and install 43 the gem to use merb. Run this command from the root of the merb svn checkout 44 to do that: 45 46 $ sudo rake install 47 48 49 If you installed merb from gems then unpack the gem and grab the sample app 50 $ gem unpack merb 51 52 now with either the svn or the unpacked gem you can try out the sample app. 53 54 $ cd merb* 55 $ cd examples/sample_app 56 57 $ merb 58 59 To use the upload example at /files you will need to add a cli flag 60 so use the -f flag to load a config file for merb_upload_progress 61 $ merb -f dist/conf/mup.conf 62 63 then the sample app will be running on port 4000 in the foreground. 64 you can go to a few urls: 65 http://localhost:4000/files/start 66 67 http://localhost:4000/foo/123/baz/12234345 22 $ rake install 23 24 To generate a new merb app after the gem is installed: 25 $ merb -g myapp 26 68 27 69 28 **FEATURES** … … 112 71 layouts. It will look for a layout named after your controller class first and 113 72 then fall back to application.herb if no layout exists named after your controller. 114 You can use render_no_layout or do layout :none right before you render73 You can use render_no_layout or do render :layout => :none 115 74 116 75 … … 123 82 end 124 83 125 <html>126 <head>127 <title>Hello, <%= @name %></title>128 </head>129 <body>130 <%= @layout_content %>131 </body>132 </html>133 134 135 <h1><%= params.inspect %></h1>136 <h1><%= cookies.inspect %></h1>137 <% 5.times do %>138 <h1>Hello, <%= @name %>!</h1>139 <% end %>140 141 84 You can also render partials like so: 142 85 <%= partial(:comments) %> 143 144 86 This assumes a _comments.rhtml file in the same view dir as the current 145 87 controller/view 88 89 Partials compile the template ands returns a string. So you can also call 90 them and assign them to a var if you want: 91 92 def someaction 93 @one = partial(:one) 94 @two = partial(:two) 95 end 96 97 partials can also render views from other controllers by specifying the path 98 99 partial('/shared/foo') 100 146 101 147 102 Merb also allows for returning javascript instead of html for ajax actions … … 167 122 168 123 169 *Controllers also now have beforefilters*124 *Controllers have powerful before and after filters* 170 125 171 126 Use the before method in your controllers. before accepts either a symbol 172 127 or a Proc/lambda object. If you give it a symbol it will call a method with 173 128 the same name as the symbol. If you give it a proc that takes one argument 174 it will call the proc with the current controller as that argument. 129 it will call the proc with the current controller as that argument. You can 130 use :only and :exclude as options to your filters to exclude or include actionsfrom certain filters. :only and :exclude take :symbols or [:sym, :sam] 131 array of symbols. 175 132 176 133 class Foo < Merb::Controller 177 134 178 before :setup_user 179 before lambda {|c| c.headers['X-Foo] = 'bar' } 135 before :setup_user, :only => :foo 136 before lambda {|c| c.headers['X-Foo] = 'bar' }, :exclude => [:foo, :baz] 180 137 181 138 def setup_user … … 183 140 end 184 141 142 def foo 143 # blah 144 end 145 185 146 def regular_action 186 147 # blah … … 189 150 end 190 151 191 Sessions are available when you start merb with the sql_session set to true or the memory_session set to true. See sample app for 192 migration too add session table. 152 To stop the before filter chain you use throw :halt with a few options: 153 154 # halts the filter chain and calls filters_halted which you can override 155 # in your controller to specialize it. 156 157 throw :halt 158 159 # halts the filters and calls the method named after the symbol: 160 161 throw :halt, :other_action 162 163 # halts the filter chain and returns the result of the Proc being called 164 165 throw :halt, Proc.new{ |c| c.redirect "/foo" } 166 167 # halts the chain and returns whatever is in the string 168 169 throw :halt, "<h1>You don't have permissions IDIOT!</h1>" 170 171 or even render templates: 172 173 throw :halt, render 'foo' 174 throw :halt, partial 'foo' 175 176 After filters only accept a Proc and call that proc with the controller: 177 178 after Proc.new {|c| Tidy.new(c.body) }, :only => :index 179 180 Sessions are available when you start merb with the sql_session set to true or the memory_session set to true. See generated app for migration too add session table. 181 182 Helpers: dist/app/helpers/global_helper.rb will be available to all of your views. Helpers named afdter your controller plus _helper.rb will be included in the views for that controller only. 193 183 194 184 … … 216 206 *File uploads* 217 207 This is one of the things that Merb was written for. Rails doesn't allow 218 multiple concurrent file uploads at once without blocking an entire rails backend 219 for each file upload. Merb allows multiple file uploads at once. Start the server 220 and browse to http://localhost:4000/files/start and you will get a file upload 221 form. This uses the mongrel_upload_progress gem so that must be installed first. 222 There is a very simple upload controller example that handles this upload with a 208 multiple concurrent file uploads at once without blocking an entire rails backend for each file upload. Merb allows multiple file uploads at once. 223 209 progress bar. When a file is uploaded with Merb, it gets put in a Tempfile. So 224 210 you just want to copy it to the right place on the filesystem. … … 243 229 will put the dist dir into another empty MERB_ROOT on the production server. 244 230 245 rm -rf project 246 mkdir -p project/{log,plugins,script,test/{specs,unit},dist/{conf,lib,plugins,schema/migrations,public/{javascripts,images,stylesheets},app/{controllers,helpers,models,views/layouts}}} 247 mate project 248 249 app_skeleton 231 merb_app: 250 232 Rakefile 251 233 README 252 234 scripts 253 235 test 254 test_helper.rb255 fixtures256 236 spec 257 237 unit trunk/lib/merb.rb
r125 r156 11 11 require 'json' 12 12 end 13 13 14 14 15 module Merb … … 94 95 require "merb/session/merb_ar_session" 95 96 include ::Merb::SessionMixin 97 t = Thread.new{ loop{ sleep(60*60); ActiveRecord::Base.verify_active_connections! } }.priority = -10 96 98 end 97 99
