[ Pobierz całość w formacie PDF ]
.A similar construct online 5 takes care of generating the title link.In both cases, the first lambda should return the output of a calltoimage_tagand the second should return a URL.In all of these lambda usages, theitemcurrently beingrendered is passed to the lambdas as a block variable.Wilson Things likelink.call(item)could potentially look even sassier aslink[item], except that you llshoot your eye out doing it.(Proc#[]is an alias forProc#call.)11.23.2.2 The New Tiled Helper MethodIf you now direct your attention to Listing [lst:11-13], you ll notice that thetiled method is changedconsiderably.In order to keep my positional argument list down to a manageable size, I ve switched overto taking a hash of options as the last parameter to thetiledmethod.This approach is useful and it mimicsthe way that almost all helper methods take options in Rails.Default values are provided for all parameters, and they are all passed along to the partial via the:localshash given torender.¹x If you re familiar with Ruby already, you might know that Proc.new is an alternate way to create anonymous blocks of code.I prefer lambda,at least in Ruby 1.9, because of subtle behavior differences.Lambda blocks check the arity of the argument list passed to them when call is invoked,and explicitly calling return in a lambda block works correctly.All About Helpers 3771 module ApplicationHelper23 def tiled(collection, opts={})45 opts[:columns] ||= 367 opts[:thumbnail] ||= lambda do |item|8 image_tag(item.photo.url(:thumb))9 end1011 opts[:title] ||= lambda {|item| item.to_s }1213 opts[:description] ||= lambda {|item| item.description }1415 opts[:link] ||= lambda {|item| item }1617 render "shared/tiled_table",18 :collection => collection,19 :columns => opts[:columns],20 :link => opts[:link],21 :thumbnail => opts[:thumbnail],22 :title => opts[:title],23 :description => opts[:description]24 end25 endFinally, to wrap up this example, here s a snippet showing how to invoke our newtiledhelper method froma template, overriding the default behavior for links:1 tiled(cities, :link => lambda {|city| showcase_city_path(city)})Theshowcase_city_pathmethod is available to the lambda block, since it is a closure, meaning that it inheritsthe execution context in which it is created.11.24 ConclusionThis very long chapter served as a thorough reference of helper methods, both those provided by Railsand ideas for ones that you will write yourself.Effective use of helper methods lead to more elegant andmaintainable view templates.At this point you should also have a good overview about how I18n support inRuby on Rails works and are ready to start translating your project.Before we fully conclude our coverage of Action Pack, (the name used to refer to Action Controller and ActionView together), we ll jump into the world of Ajax and JavaScript.Arguably, one of the main reasons for Rails scontinued popularity is its support for those two crucial technologies of Web 2.All About Helpers 378This chapter is published under the Creative Commons Attribution-ShareAlike 3.0 license, http://creativecommons.org/licenses/b-sa/3.012 Ajax on RailsAjax isn t a technology.It s really several technologies, each flourishing in its own right, comingtogether in powerful new ways Jesse J.Garrett, who coined the termAjax is an acronym that stands for _A_synchronous _JA_vascript and _X_ml.It allows us to give web pagesthe ability to be responsive and allows us to break the normal HTTP request life-cycle (without a page refresh).Some example use-cases for Ajax techniques are" Type ahead input suggestion, as in Google search" Sending form data asynchronously" Seamless navigation of web-presented maps, as in Google Maps" Dynamically updated lists and tables, as in Gmail and other web-based email services" Web-based spreadsheets" Forms that allow in-place editing" Live preview of formatted writing alongside a text inputAjax is made possible by theXMLHttpRequestObject(or XHR for short), an API that is available in all modernbrowsers.It allows JavaScript code on the browser to exchange data with the server and use it to change theuser interface of your application on the fly, without needing a page refresh.Working directly with XHRin a cross-browser-compatible way is difficult, to say the least, however we are lucky as the open-sourceecosystem flourishes with Ajax JavaScript libraries.Incidentally, Ajax, especially in Rails, has very little to do with XML, despite its presence there at the end ofthe acronym.In fact, by default Rails 4 does not include XML parsing (however, this can be reenabled).Thepayload of those asynchronous requests going back and forth to the server can be anything.Often it s just amatter of form parameters posted to the server, and receiving snippets of HTML back, for dynamic insertioninto the page s DOM.Many times it even makes sense for the server to send back data encoded in a simplekind of JavaScript called JavaScript Object Notation (JSON).It s outside the scope of this book to teach you the fundamentals of JavaScript and/or Ajax.It s also outsideof our scope to dive into the design considerations of adding Ajax to your application, elements of which arelengthy and occasionally controversial.Proper coverage of those subjects would require a whole book andthere are many such books to choose from in the marketplace
[ Pobierz całość w formacie PDF ]