Home arrow Ruby-on-Rails arrow Page 2 - Action Pack Request Parameters
RUBY-ON-RAILS

Action Pack Request Parameters


In this eighth part of a series on the Action Pack library for Rails, you will learn how to handle request parameters and how to use the render method. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
April 21, 2011
TABLE OF CONTENTS:
  1. · Action Pack Request Parameters
  2. · Rendering Responses

print this article
SEARCH DEVARTICLES

Action Pack Request Parameters - Rendering Responses
(Page 2 of 2 )

You already know that, by default, when an action hascompleted, it will attempt to render a template of the same name.Sometimes you want to render a different template, though.

In thecreateaction we just created, if the@user.savesucceeds, we want to set a friendlyflashmessage and redirect. However, if thesavefails, we want to render thenewtemplate. If we didnít explicitly rendernew, the action would fall through to its default behavior and attempt to render a template namedcreate, which, of course, doesnít exist.

Therendermethod takes several options for its first argument::text,:nothing,:inline, and:update.


Note  The:updateresponse is fairly specialized. You use it when youíre rendering Ajax responses, as youíll learn in Chapter 7.


Redirecting

It might not sound like it, but a redirection is aresponse. Redirects donít really happen on the server side. Instead, aresponse is sent to your browser that tells it to perform a redirectionto another URL. The specifics of issuing a redirect arenít somethingyou need to worry about, though, since Rails provides a specializedmethod to take care of the internals. That method is called redirect_to, and itís one youíll find yourself using a lot, so itís a good idea to get familiar with it.

As you can see from the example in thecreate action,redirect_totakes a hash of options. Most of the time, youíll specify the name of acontroller, action, or both. The current controller is presumed unlessa controller value is given, so in our case, Rails will redirect to theindexaction in theuserscontroller. You can also pass aroundparameters when redirecting; any key/value pairs you append to theredirect will be parameterized and become part of theparamshash.

Please check back for the next part of this series.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials