Module Ardes::ResourcesController::Helper
In: lib/ardes/resources_controller/helper.rb

Often it won‘t be appropriate to re-use views, but sometimes it is. These helper methods enable reuse by referencing whatever resource the controller is for.

Example:

instead of writing:

 <% for event in @events %>
   <%= link_to 'edit', edit_event_path(event) %>

you may write:

 <% for event in resources %>
   <%= link_to 'edit', edit_resource_path(event) %>

Enclosing resource

For controllers with enclosing resources instead of writing:

 <%= link_to 'back to Forum', forum_path(@forum) %>

you may write: (which will work for any enclosing path)

 <%= link_to "back to #{enclosing_resource.class.name.titleize}", enclosing_resource_path %>

Enclosing named routes:

In addition you can reference named routes that are ‘below’ the current resource by appending resource_ to that named route.

Example: shared polymorphic view

Let‘s say you have a resource controller for tags, and you‘re writing the taggable views. In a view shared amongst taggables you can write

 <%= link_to 'tags', resource_tags_path %>
 <%= link_to 'edit tag', edit_resource_tag_path(@tag) %>

or:

 <% for taggable in resources %>
   <%= link_to 'tags', resource_tags_path(taggable) %>

Methods

Public Class methods

[Source]

    # File lib/ardes/resources_controller/helper.rb, line 43
43:       def self.included(base)
44:         base.class_eval do
45:           alias_method_chain :method_missing, :named_route_helper
46:           alias_method_chain :respond_to?, :named_route_helper
47:           delegate :resource_name, :resources_name, :resource, :resources, :enclosing_resource, :enclosing_resource_name, :to => :controller
48:         end
49:       end

Public Instance methods

print the error messages for the current resource

[Source]

    # File lib/ardes/resources_controller/helper.rb, line 88
88:       def error_messages_for_resource
89:         error_messages_for resource_name
90:       end

Calls form_for with the apropriate action and method for the resource

resource.new_record? is used to decide between a create or update action

You can optionally pass a resource object, default is to use self.resource

You may also override the url by passing :url, or pass extra options to resource path url with :url_options

Example

  <% form_for_resource do |f| %>
    <%= f.text_field :name %>
    <%= f.submit resource.new_record? ? 'Create' : 'Update'
  <% end %>

  <% for attachment in resources %>
    <% form_for_resource attachment, :html => {:multipart => true} %>
      <%= f.file_field :uploaded_data %>
      <%= f.submit 'Update' %>
    <% end %>
  <% end %>

[Source]

    # File lib/ardes/resources_controller/helper.rb, line 74
74:       def form_for_resource(*args, &block)
75:         options = args.extract_options!
76:         resource = args[0] || self.resource
77:         form_for(resource_name, resource, form_for_resource_options(resource, options), &block)
78:       end

Delegate named_route helper method to the controller. Create the delegation to short circuit the method_missing call for future invocations.

[Source]

     # File lib/ardes/resources_controller/helper.rb, line 94
 94:       def method_missing_with_named_route_helper(method, *args, &block)
 95:         if controller.resource_named_route_helper_method?(method)
 96:           self.class.send(:delegate, method, :to => :controller)
 97:           controller.send(method, *args)
 98:         else
 99:           method_missing_without_named_route_helper(method, *args, &block)
100:         end
101:       end

same API as form_for_resource

[Source]

    # File lib/ardes/resources_controller/helper.rb, line 81
81:       def remote_form_for_resource(*args, &block)
82:         options = args.extract_options!
83:         resource = args[0] || self.resource
84:         remote_form_for(resource_name, resource, form_for_resource_options(resource, options), &block)
85:       end

delegate url help method creation to the controller

[Source]

     # File lib/ardes/resources_controller/helper.rb, line 104
104:       def respond_to_with_named_route_helper?(method)
105:         respond_to_without_named_route_helper?(method) || controller.resource_named_route_helper_method?(method)
106:       end

Private Instance methods

[Source]

     # File lib/ardes/resources_controller/helper.rb, line 109
109:       def form_for_resource_options(resource, options)
110:         returning options.dup do |options|
111:           options[:html] ||= {}
112:           options[:html][:method] ||= resource.new_record? ? :post : :put
113:           args = options[:url_options] ? [options.delete(:url_options)] : []
114:           options[:url] ||= if resource.new_record?
115:             controller.resource_specification.singleton? ? resource_path(*args) : resources_path(*args)
116:           else
117:             controller.resource_specification.singleton? ? resource_path(*args) : resource_path(*([resource] + args))
118:           end
119:         end
120:       end

[Validate]