Home arrow Ruby-on-Rails arrow Page 2 - Handling HTML in Templates with Action Pack
RUBY-ON-RAILS

Handling HTML in Templates with Action Pack


In this seventeenth part of an eighteen-part series on the Action Pack library for Rails, you'll learn how and why to escape HTML in templates, and more. 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 / 2
August 04, 2011
TABLE OF CONTENTS:
  1. · Handling HTML in Templates with Action Pack
  2. · Adding Custom Helpers

print this article
SEARCH DEVARTICLES

Handling HTML in Templates with Action Pack - Adding Custom Helpers
(Page 2 of 2 )

Our events application is looking pretty good, but we would like to make it a bit more user-friendly. One thing we can do is add helpful cancel links beside each of the submit buttons on the forms, so that itís possible for users to back out of editing. We could do this by adding link_to helpers beside each button, but then we would need to do this for every form. Since weíll probably want to repeat this pattern throughout the application, this could end up being a lot of duplication. Why donít we create our own custom helper to do this for us? Listing 6-29 shows the method submit_tag_or_cancel added to the application_helper .

Listing 6-29. The app/helpers/application_helper.rb File

module ApplicationHelper # Creates a submit button with the given name with a cancel link # Accepts a hash of options in url_for format to create the cancel link def submit_tag_or_cancel(name, options={})

options = 'javascript:history.go(-1);' if options.empty? "#{submit_tag(name)} or #{link_to 'Cancel', options, :class => 'cancel' }" end end

Now letís use this helper on our forms. Open both the new and edit templates and update them so they look like Listings 6-30 and 6-31.

Listing 6-30. Updated app/views/events/new.rhtml

<h1>New event</h1>

<% form_tag :action => 'create' do %> <%= render :partial => 'form' %> <%= submit_tag_or_cancel "Create" %>

<% end %>

Listing 6-31. Updated app/views/events/edit.rhtml

<h1>Editing event</h1>

<% form_tag :action => 'update', :id => @event do %> <%= render :partial => 'form' %> <%= submit_tag_or_cancel 'Save Changes' %>

<% end %>

Please check back for the conclusion to 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