Method: Merb::Router::Resources#resource
- Defined in:
- lib/merb-core/dispatch/router/resources.rb
#resource(name, *args, &block) ⇒ Object
Behavior#resource is a route helper for defining a singular RESTful resource. It yields to a block for child routes.
Parameters
- name<String, Symbol>
-
The name of the resource.
- options<Hash>
-
Overides and parameters to be associated with the route.
Options (options)
:namespace<~to_s>: The namespace for this route. :name_prefix<~to_s>:
A prefix for the named routes. If a namespace is passed and there
isn't a name prefix, the namespace will become the prefix.
:controller<~to_s>: The controller for this route
Block parameters
- next_level<Behavior>
-
The child behavior.
Returns
- Array
-
Routes which define a RESTful single resource.
Examples
r.resource :account # will result in the typical RESTful CRUD
# shows new resource form
# GET /account/new :action => "new"
# creates resource
# POST /account/?(\.:format)?, :action => "create"
# shows resource
# GET /account/(\.:format)? :action => "show"
# shows edit form
# GET /account//edit :action => "edit"
# updates resource
# PUT /account/(\.:format)? :action => "update"
# shows deletion confirmation page
# GET /account//delete :action => "delete"
# destroys resources
# DELETE /account/(\.:format)? :action => "destroy"
You can optionally pass :namespace and :controller to refine the routing or pass a block to nest resources.
r.resource :account, :namespace => "admin" do |account|
account.resources :preferences, :controller => "settings"
end
:api: public
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/merb-core/dispatch/router/resources.rb', line 227 def resource(name, *args, &block) name = name.to_s = (args) || {} params = { :controller => .delete(:controller) || name.pluralize } member = { :new => :get, :edit => :get, :delete => :get }.merge(.delete(:member) || {}) [:name_prefix] ||= nil # Don't use a name_prefix if not needed [:resource_prefix] ||= nil # Don't use a resource_prefix if not needed [:controller_prefix] ||= .delete(:namespace) self.namespace(name, ).to(params) do |resource| # => show resource.match("(.:format)", :method => :get).to(:action => "show"). name(name).register_resource(name) # => create resource.match("(.:format)", :method => :post).to(:action => "create") # => update resource.match("(.:format)", :method => :put).to(:action => "update") # => destroy resource.match("(.:format)", :method => :delete).to(:action => "destroy") member.each_pair do |action, method| action = action.to_s resource.match("/#{action}(.:format)", :method => method).to(:action => action). name(action, name).register_resource(name, action) end if block_given? builders = {} builders[:member] = lambda do |action, to, method| resource.match("/#{action}(.:format)", :method => method).to(:action => to). name(action, name).register_resource(name, action) end resource.(:name_prefix => name, :resource_prefix => name). resource_block(builders, &block) end end end |