github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

collectiveidea / with_action

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 20
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Click here to lend your support to: with_action and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A respond_to style helper for doing different actions based on which request parameters are passed. — Read more

  cancel

http://daniel.collectiveidea.com/blog/tags/random_finders

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Allow #with_action to take actions as arguments, which get called as 
methods on the controller 
brandon (author)
Sat Dec 12 22:50:09 -0800 2009
commit  9a5178c513387843e3f2cb13c58fca1d378d3db5
tree    10764907cba60eaa67b83bd7ce2142c083a1ce30
parent  4a73019b8f935e34683cb7f0b6d8410cc40d55db
with_action /
name age
history
message
file MIT-LICENSE Loading commit data...
file README
file Rakefile
file init.rb
directory lib/
directory tasks/
directory test/
README
= WithAction

A respond_to style helper for doing different actions based on which request parameters are passed. Specifically, it is 
helpful if you want to use multiple form buttons on a page, such as "Save", "Save and Continue Editing", and "Cancel".  
with_action executes different blocks based on what the presence of request parameters.

  def create
    with_action do |a|
      a.cancel { redirect_to articles_path }
      a.any do
        @article = Article.new(params[:article])
        if @article.save
          a.save { redirect_to article_path(@article) }
          a.edit { redirect_to article_path(@article) }
          a.approve do
            @article.approve!
            redirect_to article_path(@article)
          end
        else
          render :action => 'new'
        end
      end
    end
  end
  
A block is invoked if a parameter with the same name exists and is not blank.  Here is an example of a form to submit to 
this action:

  <%= submit_tag 'Save', :name => 'save' %>
  <%= submit_tag 'Save & Continue Editing', :name => 'edit' %>
  <%= submit_tag 'Save & Approve', :name => 'approve' %>
  <%= submit_tag 'Cancel', :name => 'cancel' %>

If an @any@ block is present and no parameter that matches one of the other blocks, it is called by default, otherwise 
the first block will be called. The @any@ block is the only one that can have nesting and be called multiple times.

If a block is not passed to the action, then a method with the same name is called on the controller: 

  def update
    with_action do |a|
      a.publish
      a.reject
      a.any { redirect_to root_path }
    end
  end
  
  def publish
    # …
  end
  
  def reject
    # …
  end
  
Which can be abbreviated as:

  def update
    with_action(:publish, :reject) do |a|
      a.any { redirect_to root_path }
    end
  end

(c) Copyright 2007 Brandon Keepers (brandon@opensoul.org)
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server