Requested
Rails 3 ActionController::Request improvements with several methods to get info on requested actions and resources.
Use It
In your controller
if request.action.collection?
@collection = request.resource.all
elsif request.action.single_object?
@object = request.resource.find params[:id]
end
In your views
<% if request.action.new_or_create? %>
<%= f.input :image, :required => true %>
<% else %>
<%= f.input :image, :required => true, :hint => button('crop', crop_image_path(@object)) %>
<% end %>
Installation
As a gem:
Add this line to your environment.rb
:
config.gem "requested"
and then do
rake gems:install
or just
gem install requested
As a plugin
script/plugin install git://github.com/mcasimir/requested.git
Usage
Test if user agent is a robot
request.is_robot?
Controller and Action names
request.controller
request.action
Action details
# Test if action is one of index, new, create, edit, update, destroy, show:
request.action.rest?
# Test single restful actions
request.action.index?
request.action.new?
request.action.create?
request.action.edit?
request.action.update?
request.action.destroy?
# Test if action is for a collection (index) or for a single resource:
request.collection?
request.single_object?
# Creating resources
request.new_or_create?
# Updating resources
request.edit_or_update?
Resource
# Retrieve the resource model class belonging to current controller
# eg. ArticlesController --> Article
request.resource
# Retrieve the name of the resource belonging to current controller
# eg. ArticlesController --> "article"
request.resource_name
# Retrieve the collection name of the resource belonging to current controller
# eg. ArticlesController --> "articles"
request.collection_name
Project Details
Copyright © 2010 Maurizio Casimirri, released under the LGPL license.