Sha256: 50cc2ae88feacf27a43e4495f5bc5e6b20cc93af4bd3c79bffda5d1bae20a3ab

Contents?: true

Size: 1.84 KB

Versions: 47

Compression:

Stored size: 1.84 KB

Contents

# Controller::Actions

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

```ruby
prepend Actions
```

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

```ruby
prepend Rewrite, Actions
```

A simple CRUD controller might look like:

```ruby
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><code class="language-ruby">"*"</code></dt>
	<dd>Match a single path element</dd>
	<dt><code class="language-ruby">"**"</code></dt>
	<dd>Match all remaining path elements</dd>
	<dt><code class="language-ruby">String</code></dt>
	<dd>Match a named path component, e.g. <code class="language-ruby">"edit"</code>.</dd>
	<dt><code class="language-ruby">Symbol</code></dt>
	<dd>Equivalent to <code class="language-ruby">["**", symbol.to_s]</code>, e.g. <code class="language-ruby">:logout</code>.</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:

```ruby
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

47 entries across 47 versions & 1 rubygems

Version Path
utopia-2.13.4 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.13.3 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.13.2 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.13.1 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.13.0 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.12.4 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.12.3 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.12.2 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.12.1 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.12.0 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.11.1 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.11.0 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.10.0 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.9.5 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.9.3 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.9.2 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.9.1 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.9.0 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.8.2 documentation/pages/wiki/middleware/controller/actions/content.md
utopia-2.8.1 documentation/pages/wiki/middleware/controller/actions/content.md