Sha256: 5c98a4a1767235c991e5def814c2c89a4b4155e7e8b112f12f831279d35968b0

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# Controller::Actions

Actions let you match path patterns in your controller and execute code. In your `controller.rb` simply add:

	prepend Actions

If you are adding multiple things, like rewriting, they should come earlier in the chain, e.g:

	prepend Rewrite, Actions

A simple CRUD controller might look like:

	prepend Actions
	
	on 'index' do
		@users = User.all
	end
	
	on 'new' do |request|
		@user = User.new
		
		if request.post?
			@user.update_attributes(request.params['user'])
			
			redirect! "index"
		end
	end
	
	on 'edit' do |request|
		@user = User.find(request.params['id'])
		
		if request.post?
			@user.update_attributes(request.params['user'])
			
			redirect! "index"
		end
	end
	
	on 'delete' do |request|
		User.find(request.params['id']).destroy
		
		redirect! "index"
	end
	
## Path Matching

Path matching works from right to left, and '**' is a greedy operator. Controllers are invoked with a path relative to the controller's `URI_PATH`, so all lookups are relative to the controller.

<dl>
	<dt>"*"</dt>
	<dd>Match a single path element</dd>
	<dt>"**"<dt>
	<dd>Match all remaining path elements</dd>
	<dt>String</dt>
	<dd>Match a named path component</dd>
	<dt>Symbol</dt>
	<dd>Equivalent to ["**", symbol.to_s]</dd>
</dl>

## Otherwise Matching

If no action was matched, it is sometimes useful to perform some specific behaviour. You can specify this by using the otherwise handler:

	otherwise do |request, path|
		fail! :teapot
	end

If you are doing this to perform some kind of rewriting, it may be preferable to use the [Rewrite](rewrite/) controller layer.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
utopia-1.9.2 documentation/pages/wiki/controller/actions/content.md