Changeset 7400
- Timestamp:
- 09/02/07 23:53:31 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_view/helpers/prototype_helper.rb
r7220 r7400 181 181 end 182 182 183 # Works like form_remote_tag, but uses form_for semantics. 184 def remote_form_for(record_or_name, *args, &proc) 183 # Creates a form that will submit using XMLHttpRequest in the background 184 # instead of the regular reloading POST arrangement and a scope around a 185 # specific resource that is used as a base for questioning about 186 # values for the fields. 187 # 188 # === Resource 189 # 190 # Example: 191 # <% remote_form_for(@post) do |f| %> 192 # ... 193 # <% end %> 194 # 195 # This will expand to be the same as: 196 # 197 # <% remote_form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> 198 # ... 199 # <% end %> 200 # 201 # === Nested Resource 202 # 203 # Example: 204 # <% remote_form_for([@post, @comment]) do |f| %> 205 # ... 206 # <% end %> 207 # 208 # This will expand to be the same as: 209 # 210 # <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), :html => { :method => :put, :class => "edit_comment", :id => "edit_comment_45" } do |f| %> 211 # ... 212 # <% end %> 213 # 214 # If you don't need to attach a form to a resource, then check out form_remote_tag. 215 # 216 # See FormHelper#form_for for additional semantics. 217 def remote_form_for(record_or_name_or_array, *args, &proc) 185 218 options = args.extract_options! 186 219 187 case record_or_name 220 case record_or_name_or_array 188 221 when String, Symbol 189 object_name = record_or_name 222 object_name = record_or_name_or_array 223 when Array 224 object = record_or_name_or_array.last 225 object_name = ActionController::RecordIdentifier.singular_class_name(object) 226 apply_form_for_options!(record_or_name_or_array, options) 227 args.unshift object 190 228 else 191 object = record_or_name 192 object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name )229 object = record_or_name_or_array 230 object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array) 193 231 apply_form_for_options!(object, options) 194 232 args.unshift object trunk/actionpack/test/template/prototype_helper_test.rb
r6967 r7400 9 9 def name 10 10 @id.nil? ? 'new author' : "author ##{@id}" 11 end 12 end 13 14 class Article 15 attr_reader :id 16 attr_reader :author_id 17 def save; @id = 1; @author_id = 1 end 18 def new_record?; @id.nil? end 19 def name 20 @id.nil? ? 'new article' : "article ##{@id}" 11 21 end 12 22 end … … 31 41 include ActionView::Helpers::CaptureHelper 32 42 include ActionView::Helpers::RecordIdentificationHelper 43 include ActionController::PolymorphicRoutes 33 44 34 45 def setup … … 60 71 61 72 def setup 62 @record = Author.new 73 @record = @author = Author.new 74 @article = Article.new 63 75 super 64 76 end … … 115 127 remote_form_for(@record, {:html => { :id => 'create-author' }}) {} 116 128 117 expected = %(<form action='#{authors_ url}' onsubmit="new Ajax.Request('#{authors_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>)129 expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>) 118 130 assert_dom_equal expected, _erbout 119 131 end … … 123 135 remote_form_for(@record) {} 124 136 125 expected = %(<form action='#{authors_ url}' onsubmit="new Ajax.Request('#{authors_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>)137 expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>) 126 138 assert_dom_equal expected, _erbout 127 139 end … … 132 144 remote_form_for(@record) {} 133 145 134 expected = %(<form action='#{author_url(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_url(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 146 expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 147 assert_dom_equal expected, _erbout 148 end 149 150 def test_remote_form_for_with_new_object_in_list 151 _erbout = '' 152 remote_form_for([@author, @article]) {} 153 154 expected = %(<form action='#{author_articles_path(@author)}' onsubmit="new Ajax.Request('#{author_articles_path(@author)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_article' method='post' id='new_article'></form>) 155 assert_dom_equal expected, _erbout 156 end 157 158 def test_remote_form_for_with_existing_object_in_list 159 @author.save 160 @article.save 161 _erbout = '' 162 remote_form_for([@author, @article]) {} 163 164 expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 135 165 assert_dom_equal expected, _erbout 136 166 end … … 234 264 235 265 protected 236 def author_ url(record)266 def author_path(record) 237 267 "/authors/#{record.id}" 238 268 end 239 269 240 def authors_ url270 def authors_path 241 271 "/authors" 242 272 end 243 244 def polymorphic_path(record)245 if record.new_record?246 "/authors"247 else248 "/authors/#{record.id}"249 end273 274 def author_articles_path(author) 275 "/authors/#{author.id}/articles" 276 end 277 278 def author_article_path(author, article) 279 "/authors/#{author.id}/articles/#{article.id}" 250 280 end 251 281 end