Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8626

Show
Ignore:
Timestamp:
01/11/08 16:25:23 (6 months ago)
Author:
david
Message:

Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used (closes #10739) [jeremyevans]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8625 r8626  
    11*SVN* 
     2 
     3* Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used #10739 [jeremyevans] 
    24 
    35* Provide nicer access to HTTP Headers.  Instead of request.env["HTTP_REFERRER"] you can now use request.headers["Referrer"]. [Koz] 
  • trunk/actionpack/lib/action_view/helpers/active_record_helper.rb

    r8466 r8626  
    5757      #     form << collection_select("department", "id", @departments, "id", "name") 
    5858      #   end 
     59      # 
     60      # The following options are available: 
     61      # 
     62      # * <tt>action</tt> - the action used when submitting the form (default: create if a new record, otherwise update) 
     63      # * <tt>input_block</tt> - specialize the output using a different block, see above 
     64      # * <tt>method</tt> - the method used when submitting the form (default: post) 
     65      # * <tt>multipart</tt> - whether to change the enctype of the form to multipart/form-date, used when uploading a file (default: false) 
     66      # * <tt>submit_value</tt> - the text of the submit button (default: Create if a new record, otherwise Update) 
    5967      def form(record_name, options = {}) 
    6068        record = instance_variable_get("@#{record_name}") 
     
    6674        submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize 
    6775 
    68         contents = '' 
     76        contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil) 
    6977        contents << hidden_field(record_name, :id) unless record.new_record? 
    7078        contents << all_input_tags(record, record_name, options) 
    7179        yield contents if block_given? 
    7280        contents << submit_tag(submit_value) 
    73  
    74         content_tag('form', contents, :action => action, :method => 'post', :enctype => options[:multipart] ? 'multipart/form-data': nil) 
     81        contents << '</form>' 
    7582      end 
    7683 
  • trunk/actionpack/test/template/active_record_helper_test.rb

    r8564 r8626  
    8787    @user.email = "" 
    8888  end 
     89   
     90  def protect_against_forgery? 
     91    @protect_against_forgery ? true : false 
     92  end 
     93  attr_accessor :request_forgery_protection_token, :form_authenticity_token 
    8994 
    9095  def setup 
     
    141146    ) 
    142147  end 
     148   
     149  def test_form_with_protect_against_forgery 
     150    @protect_against_forgery = true 
     151    @request_forgery_protection_token = 'authenticity_token' 
     152    @form_authenticity_token = '123' 
     153    assert_dom_equal( 
     154      %(<form action="create" method="post"><div style='margin:0;padding:0'><input type='hidden' name='authenticity_token' value='123' /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>), 
     155      form("post") 
     156    ) 
     157  end 
     158   
     159  def test_form_with_method_option 
     160    assert_dom_equal( 
     161      %(<form action="create" method="get"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>), 
     162      form("post", :method=>'get') 
     163    ) 
     164  end 
    143165 
    144166  def test_form_with_action_option