Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

with_action

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

with_action

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
2
Created
Source

= 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)

FAQs

Package last updated on 26 Oct 2010

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc