Path: | vendor/rails/actionpack/CHANGELOG |
Last Update: | Thu Aug 04 22:20:26 UTC 2005 |
*1.9.1* (11 July, 2005)
*1.9.0* (6 July, 2005)
<%= update_element_function("products", :action => :insert, :position => :bottom, :content => "<p>New product!</p>") %> <% update_element_function("products", :action => :replace, :binding => binding) do %> <p>Product 1</p> <p>Product 2</p> <% end %>
xml.cdata! "some text" # => <![CDATA[some text]]>
link_to_remote( "test", :url => { :action => "faulty" }, :update => { :success => "good", :failure => "bad" }, 403 => "alert('Forbidden- got ya!')", 404 => "alert('Nothing there...?')", :failure => "alert('Unkown error ' + request.status)")
form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") }
<% show_calendar(:year => 2005, :month => 6) do |day, options| %> <% options[:bgcolor] = '#dfd' if 10..15.include? day %> [<%= day %>] <% end %>
+---------------------------------------------------------------+-------------------------------------------------------+ | BEFORE | AFTER | +---------------------------------------------------------------+-------------------------------------------------------+ | render_with_layout "weblog/show", "200 OK", "layouts/dialog" | render :action => "show", :layout => "dialog" | | render_without_layout "weblog/show" | render :action => "show", :layout => false | | render_action "error", "404 Not Found" | render :action => "error", :status => "404 Not Found" | | render_template "xml.div('stuff')", "200 OK", :rxml | render :inline => "xml.div('stuff')", :type => :rxml | | render_text "hello world!" | render :text => "hello world!" | | render_partial_collection "person", @people, nil, :a => 1 | render :partial => "person", :collection => @people, | | | :locals => { :a => 1 } | +---------------------------------------------------------------+-------------------------------------------------------+
Content-Type: application/xml <request><item><content>HelloWorld</content></item></request>
…is the same as:
Content-Type: application/x-yaml --- item: content: HelloWorld
…is the same as:
item[content]=HelloWorld
Which in the end turns into { "item" => { "content" => "HelloWorld" } }. This makes it a lot easier to publish REST web services on top of your regular actions (as they won’t care).
Example Curl call:
curl -H 'Content-Type: application/xml' -d '<request><item><content>KillMeMore</content></item></request>' http://www.example.com/service
You can query to find out whether a given request came through as one of these types with:
- request.post_format? (:url_encoded, :xml or :yaml) - request.formatted_post? (for either xml or yaml) - request.xml_post? - request.yaml_post?
<% benchmark "Notes section" do %> <%= expensive_notes_operation %> <% end %> Will add something like "Notes section (0.345234)" to the log.
def after_save(record) @list = record.is_a?(List) ? record : record.list end def filter(controller) controller.expire_page(:controller => "lists", :action => %w( show public feed ), :id => @list.id) controller.expire_action(:controller => "lists", :action => "all") @list.shares.each { |share| controller.expire_page(:controller => "lists", :action => "show", :id => share.url_key) } end
..after:
def after_save(record) list = record.is_a?(List) ? record : record.list expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id) expire_action(:controller => "lists", :action => "all") list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) } end
The new sweepers can also observe on the actions themselves by implementing methods according to (before|after)_$controller_$action. Example of a callback that’ll be called after PagesController#update_title has been performed:
def after_pages_update_title expire_fragment(%r{pages/#{controller.assigns["page"].id}/.*}) end
Note that missing_method is delegated to the controller instance, which is assigned in a before filter. This means that you can call expire_fragment instead of @controller.expire_fragment.
*1.8.1* (20th April, 2005)
xhr :post, :index
*1.8.0* (19th April, 2005)
* Effect.Puff zooms the element out and makes it smoothly transparent at the same time, giving a "puff" illusion #996 [thomas@fesch.at] After the animation is completed, the display property will be set to none. This effect will work on relative and absolute positioned elements. * Effect.Appear as the opposite of Effect.Fade #990 [thomas@fesch.at] You should return elements with style="display:none;" or a like class for this to work best and have no chance of flicker. * Effect.Squish for scaling down an element and making it disappear at the end #972 [thomas@fesch.at] * Effect.Scale for smoothly scaling images or text up and down #972 [thomas@fesch.at] * Effect.Fade which smoothly turns opacity from 100 to 0 and then hides the element #960 [thomas@fesch.at]
<%= render_partial "account", person, "rules" => regulations.rules %>
…to:
<%= render_partial "account", :account => person, :rules => regulations.rules %>
The old API will still work, though, and render_partial "account" will still assume :account => @account.
*1.7.0* (27th March, 2005)
*1.6.0* (22th March, 2005)
class SomethingController < ApplicationController def save ... if @something.save # will redirect, use flash flash[:message] = 'Save successful' redirect_to :action => 'list' else # no redirect, message is for current action, use flash.now flash.now[:message] = 'Save failed, review' render_action 'edit' end end end
class Page def initialize(number) @number = number end # ... def to_param @number.to_s end end
You can now use instances of Page with url_for:
class BarController < ApplicationController def baz page = Page.new(4) url = url_for :page => page # => "http://foo/bar/baz?page=4" end end
map.connect 'categories/*path_info', :controller => 'categories', :action => 'show'
A request for /categories/top-level-cat, would give @params[:path_info] with "top-level-cat". A request for /categories/top-level-cat/level-1-cat, would give @params[:path_info] with "top-level-cat/level-1-cat" and so forth.
The @params[:path_info] return is really an array, but where to_s has been overwritten to do join("/").
Before:
def test_list assert_equal 5, @response.template.assigns['recipes'].size assert_equal 8, @response.template.assigns['categories'].size end
After:
def test_list assert_equal 5, assigns(:recipes).size assert_equal 8, assigns(:categories).size end
*1.5.1* (7th March, 2005)
*1.5.0* (24th February, 2005)
* Fixed that proxy IPs do not follow all RFC1918 nets 251 [caleb@aei-tech.com]
date_select("post", "written_on", :order => [:day, :month, :year]) date_select("user", "birthday", :order => [:month, :day])
def test_create_post post :create, "post" => { "title" => "Exciting!" } assert_redirected_to :action => "show" follow_redirect assert_rendered_file "post/show" end
Limitation: Only works for redirects to other actions within the same controller.
*1.4.0* (January 25th, 2005)
<% @students.each do |@student| %> <%= text_field "student[]", "first_name", :size => "20" %> <%= text_field "student[]", "last_name" %> <%= text_field "student[]", "grade", :size => "5" %> <% end %>
…would produce, for say David Black with id 123 and a grace of C+:
<input id="student_123_first_name" name="student[123][first_name]" size="20" size="30" type="text" value="David" /> <input id="student_123_last_name" name="student[123][last_name]" size="30" type="text" value="Black" /> <input id="student_123_grade" name="student[123][grade]" size="5" type="text" value="C+" />
def show redirect_to(:action => "login") and return unless @person.authenticated? render_text "I won't happen unless the person is authenticated" end
class WeblogController before_filter { |c| c.send(:redirect_to_url("http://www.farfaraway.com")}) } def hello render_text "I will never be called" end end
def do_something redirect_to :action => "elsewhere" render_action "overthere" end
Only the redirect happens. The rendering call is simply ignored.
*1.3.1* (January 18th, 2005)
*1.3.0* (January 17th, 2005)
form("entry", :action => "sign") do |form| form << content_tag("b", "Department") form << collection_select("department", "id", @departments, "id", "name") end
url_for(:controller => 'user', :action => 'delete', :params => { 'username' => %( paul john steve ) } ) # => /user/delete?username[]=paul&username[]=john&username[]=steve
Example:
text_field "person", "name", "index" => 3
Becomes:
<input type="text" name="person[3][name]" id="person_3_name" value="<%= @person.name %>" />
Before:
def authenticate unless @session[:authenticated] redirect_to :controller => "account", :action => "login" return false end end
After:
def authenticate redirect_to(:controller => "account", :action => "login") unless @session[:authenticated] end
class JournalController < ActionController::Base # only require authentication if the current action is edit or delete before_filter :authorize, :only_on => [ :edit, :delete ] private def authorize # redirect to login unless authenticated end end
*1.2.0* (January 4th, 2005)
*1.1.0*
*1.0.1*
*1.0*
class SomeController < ApplicationController <% if options[:scaffold] %> scaffold :<%= singular_name %> <% end %> helper :post
…produces this on post as singular_name:
class SomeController < ApplicationController scaffold :post helper :post
…where as:
class SomeController < ApplicationController <% if options[:scaffold] -%> scaffold :<%= singular_name %> <% end -%> helper :post
…produces:
class SomeController < ApplicationController scaffold :post helper :post
[This undocumented gem for ERb was uncovered by bitsweat]
Creates a link tag to the image residing at the +src+ using an URL created by the set of +options+. See the valid options in link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to get a link tag that just points without consideration. The <tt>html_options</tt> works jointly for the image and ahref tag by letting the following special values enter the options on the image and the rest goes to the ahref: ::alt: If no alt text is given, the file name part of the +src+ is used (capitalized and without the extension) ::size: Supplied as "XxY", so "30x45" becomes width="30" and height="45" ::align: Sets the alignment, no special features The +src+ can be supplied as a... * full path, like "/my_images/image.gif" * file name, like "rss.gif", that gets expanded to "/images/rss.gif" * file name without extension, like "logo", that gets expanded to "/images/logo.png"
# Calls Controller#miletone with a GET request process :milestone # Calls Controller#miletone with a POST request that has parameters post :milestone, { "name" => "David" } # Calls Controller#milestone with a HEAD request that has both parameters and session data head :milestone, { "id" => 1 }, { "user_id" => 23 }
This is especially useful for testing idiomatic REST web services.
Examples for writing:
cookies["user_name"] = "david" # => Will set a simple session cookie cookies["login"] = { "value" => "XJ-122", "expires" => Time.now + 360} # => Will set a cookie that expires in 1 hour
Examples for reading:
cookies["user_name"] # => "david" cookies.size # => 2
Read more in ActionController::Cookies
NOTE: If you were using the old accessor (cookies instead of @cookies), this could potentially break your code — if you expect a full cookie object!
NOTE: This will only have an effect if you use the new model, service, and observer class methods to mark dependencies. All libraries loaded through require will be "forever" cached. You can, however, use ActionController::Base.load_or_require("library") to get this behavior outside of the new dependency style.
class MsgController < ApplicationController helper :msg end
…you can just do:
class MsgController < ApplicationController end
MsgController.dependencies_on(:model) # => [ :post, :comment, :attachment ] MsgController.dependencies_on(:service) # => [ :notification_service ] MsgController.dependencies_on(:observer) # => [ :comment_observer ]
class MsgController < ApplicationController model :post, :comment, :attachment service :notification_service observer :comment_observer end
These new "keywords" remove the need for explicitly calling ‘require’ in most cases. The observer method even instantiates the observer as well as requiring it.
*0.9.5* (28)
Before:
module WeblogHelper def self.append_features(controller) #:nodoc: controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super end end require 'weblog_helper' class WeblogController < ActionController::Base include WeblogHelper end
After:
module WeblogHelper end class WeblogController < ActionController::Base helper :weblog end
*0.9.0 (43)*
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do xml.channel do xml.title(@feed_title) xml.link(@url) xml.description "Basecamp: Recent items" xml.language "en-us" xml.ttl "40" for item in @recent_items xml.item do xml.title(item_title(item)) xml.description(item_description(item)) if item_description(item) xml.pubDate(item_pubDate(item)) xml.guid(@person.firm.account.url + @recent_items.url(item)) xml.link(@person.firm.account.url + @recent_items.url(item)) xml.tag!("dc:creator", item.author_name) if item_has_creator?(item) end end end end ...which will generate something like: <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <title>Web Site Redesign</title> <link>http://www.basecamphq.com/clients/travelcenter/1/</link> <description>Basecamp: Recent items</description> <language>en-us</language> <ttl>40</ttl> <item> <title>Post: don't you know</title> <description>&lt;p&gt;deeper and down&lt;/p&gt;</description> <pubDate>Fri, 20 Aug 2004 21:13:50 CEST</pubDate> <guid>http://www.basecamphq.com/clients/travelcenter/1/msg/assets/96976/comments</guid> <link>http://www.basecamphq.com/clients/travelcenter/1/msg/assets/96976/comments</link> <dc:creator>David H. Heinemeier</dc:creator> </item> <item> <title>Milestone completed: Design Comp 2</title> <pubDate>Mon, 9 Aug 2004 14:42:06 CEST</pubDate> <guid>http://www.basecamphq.com/clients/travelcenter/1/milestones/#49</guid> <link>http://www.basecamphq.com/clients/travelcenter/1/milestones/#49</link> </item> </channel> </rss>
The "xml" local variable is automatically available in .rxml templates. You construct the template by calling a method with the name of the tag you want. Options for the tag can be specified as a hash parameter to that method.
Builder-based templates can be mixed and matched with the regular ERb ones. The only thing that differentiates them is the extension. No new methods have been added to the public interface to handle them.
Action Pack ships with a version of Builder, but it will use the RubyGems version if you have one installed.
Read more about Builder on: onestepback.org/index.cgi/Tech/Ruby/StayingSimple.rdoc
[Builder is created by Jim Weirich]
# Old style def test_failing_authenticate @request.request_uri = "/login/authenticate" @request.action = "authenticate" @request.request_parameters["user_name"] = "nop" @request.request_parameters["password"] = "" response = LoginController.process_test(@request) assert_equal "The username and/or password you entered is invalid.", response.session["flash"]["alert"] assert_equal "http://37signals.basecamp.com/login/", response.headers["location"] end # New style def test_failing_authenticate process :authenticate, "user_name" => "nop", "password" => "" assert_flash_has 'alert' assert_redirected_to :action => "index" end
See a full example on codepaste.org/view/paste/334
<%= render_collection_of_partials "ad", @advertisements %> This will render "advertiser/_ad.rhtml" and pass the local variable +ad+ to the template for display. An iteration counter will automatically be made available to the template with a name of the form +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?', the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
Creates a link tag of the given +name+ using an URL created by the set of +options+, unless the current controller, action, and id are the same as the link's, in which case only the name is returned (or the given block is yielded, if one exists). This is useful for creating link bars where you don't want to link to the page currently being viewed.
<%= render_partial "advertisement/ad", ad %>
This will render the partial "advertisement/_ad.rhtml" regardless of which controller this is being called from.
[Jacob Fugal]
before_filter Proc { |controller| return false if controller.params["stop_action"] }
…can now be as:
before_filter { |controller| return false if controller.params["stop_action"] }
[Jeremy Kemper]
*0.8.5*
You can pass local variables to sub templates by using a hash of with the variable names as keys and the objects as values: <%= render "shared/header", { "headline" => "Welcome", "person" => person } %> These can now be accessed in shared/header with: Headline: <%= headline %> First name: <%= person.first_name %>
There's also a convenience method for rendering sub templates within the current controller that depends on a single object (we call this kind of sub templates for partials). It relies on the fact that partials should follow the naming convention of being prefixed with an underscore -- as to separate them from regular templates that could be rendered on their own. In the template for Advertiser#buy, we could have: <% for ad in @advertisements %> <%= render_partial "ad", ad %> <% end %> This would render "advertiser/_ad.rhtml" and pass the local variable +ad+ for the template to display. == Rendering a collection of partials The example of partial use describes a familar pattern where a template needs to iterate over a array and render a sub template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial by the same name of as the elements contained within. So the three-lined example in "Using partials" can be rewritten with a single line: <%= render_collection_of_partials "ad", @advertisements %> So this will render "advertiser/_ad.rhtml" and pass the local variable +ad+ for the template to display.
Sends the file by streaming it 4096 bytes at a time. This way the whole file doesn't need to be read into memory at once. This makes it feasible to send even large files. Be careful to sanitize the path parameter if it coming from a web page. send_file(@params['path'] allows a malicious user to download any file on your server. Options: * <tt>:filename</tt> - specifies the filename the browser will see. Defaults to File.basename(path). * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'. * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. Valid values are 'inline' and 'attachment' (default). * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used to stream the file. Defaults to 4096. The default Content-Type and Content-Disposition headers are set to download arbitrary binary files in as many browsers as possible. IE versions 4, 5, 5.5, and 6 are all known to have a variety of quirks (especially when downloading over SSL). Simple download: send_file '/path/to.zip' Show a JPEG in browser: send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline' Read about the other Content-* HTTP headers if you'd like to provide the user with more information (such as Content-Description). http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11 Also be aware that the document may be cached by proxies and browsers. The Pragma and Cache-Control headers declare how the file may be cached by intermediaries. They default to require clients to validate with the server before releasing cached responses. See http://www.mnot.net/cache_docs/ for an overview of web caching and http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 for the Cache-Control header spec.
*0.8.0*
<%= select "person", "gender", %w( Male Female ) %>
…would give the following:
<select name="person[gender]" id="person_gender"><option>Male</option><option>Female</option></select>
<input type="checkbox" name="rights[]" value="CREATE" /> <input type="checkbox" name="rights[]" value="UPDATE" /> <input type="checkbox" name="rights[]" value="DELETE" />
…and retrieved in the controller action with:
@params["rights"] # => [ "CREATE", "UPDATE", "DELETE" ]
The old behavior (where the last one wins, "DELETE" in the example) is still available. Just don’t add "[]" to the end of the name. [Scott Baron]
def play_movie
send_file "/movies/that_movie.avi"
end
[Jeremy Kemper]
render_text do |response| File.open(path, 'rb') do |file| while buf = file.read(1024) print buf end end end
[Jeremy Kemper]
tag("br", "clear" => "all") => <br clear="all" />
…that’s usually not terribly interesting (unless you have a lot of options already in a hash), but it gives way for more specific tags, like the new form tag:
form_tag({ :controller => "weblog", :action => "update" }, { :multipart => "true", "style" => "width: 200px"}) => <form action="/weblog/update" enctype="multipart/formdata" style="width: 200px">
There’s even a "pretty" version for people who don’t like to open tags in code and close them in HTML:
<%= start_form_tag :action => "update" %> # all the input fields <%= end_form_tag %>
(end_form_tag just returns "</form>")
options_for_select([ "VISA", "Mastercard", "Discover" ], ["VISA", "Discover"]) => <option selected>VISA</option>\n<option>Mastercard</option>\n<option selected>Discover</option>
[Scott Baron]
url_for(:controller_prefix => "clients")
…or:
url_for(:action_prefix => "category/messages")
Neither would have worked in isolation before (:controller_prefix required a :controller and :action_prefix required an :action)
ActionView::AbstractTemplate.field_error_proc = Proc.new do |html, instance| "<p>#{instance.method_name + instance.error_message}</p><div style='background-color: red'>#{html}</div>" end
…would give the following on a Post#title (text field) error:
<p>Title can't be empty</p> <div style='background-color: red'> <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /> </div>
*0.7.9*
class WeblogController < ActionController::Base layout "layouts/weblog" scaffold :post end
[Suggested by Scott]
class WeblogController < ActionController::Base def update # do some update redirect_to :dashboard_url end protected def dashboard_url if @project.active? url_for :controller => "project", :action => "dashboard" else url_for :controller => "account", :action => "dashboard" end end end
Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in form of a hash, just like the one you would use for url_for directly. Example: def default_url_options(options) { :controller_prefix => @project.active? ? "projects/" : "accounts/" } end As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic dissions about the urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set by this method.
url_for(:action => "show", :params => { "id" => 5 }) ...used to give http://localhost:81/friends/show/5 ......now gives http://localhost:81/friends/show?id=5 If you want the automated id behavior, do: url_for(:action => "show", :id => 5 ) ....which gives http://localhost:81/friends/show/5
*0.7.8*
class WeblogController < ActionController::Base around_filter BenchmarkingFilter.new # Before this action is performed, BenchmarkingFilter#before(controller) is executed def index end # After this action has been performed, BenchmarkingFilter#after(controller) is executed end class BenchmarkingFilter def initialize @runtime end def before start_timer end def after stop_timer report_result end end
text_field "post", "title" ...just gives: <input id="post_title" name="post[title]" size="30" type="text" value="" /> text_field "post", "title", "id" => "title_for_post", "name" => "first_post_title" ...can now give: <input id="title_for_post" name="first_post_title" size="30" type="text" value="" />
*0.7.7*
*0.7.6*
url_for(:action => "list") ...used to give http://localhost:81/friends/list/1 ......now gives http://localhost:81/friends/list url_for(:controller => "friends", :action => "destroy", :id => 5) ...used to give http://localhost:81/friends/destroy ......now gives http://localhost:81/friends/destroy/5
Considering the url localhost:81/teachers/show/t
url_for(:action => "list", :id => 5) ...used to give http://localhost:81/5eachers/list/t ......now gives http://localhost:81/teachers/list/5
[Reported by David Morton & Radsaq]
*0.7.5*