This project is archived and is in readonly mode.
ActiveResource generates an error for a form_for attribute
Reported by Conrad Taylor | March 11th, 2010 @ 10:56 PM
Hi, ActiveResource generates the following error when I attempt to create a new resource instance:
ActionView::TemplateError (undefined method `name' for #<Product:0x1034edcd0>) on line #7 of app/views/products/new.html.erb:
4: <%= f.error_messages %>
5: <p>
6: <%= f.label :name %><br />
7: <%= f.text_field :name %>
I have the following product resource model and associated view:
/app/models/product.rb:
class Product < ActiveResource::Base
self.site = "http://api.example.com:3001/"
end
/app/views/products/new.html.erb:
<h1>New product</h1>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
Next, the relevant controller code.
/app/controllers/products_controller.rb:
...
# GET /products/new
# GET /products/new.xml
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
...
My hardware and software details are as follows:
$ ruby -v ruby 1.8.7 (2008-08-11 patchlevel 72)
[universal-darwin10.0]
$ rails -v Rails 2.3.5
$ gem -v 1.3.6
Comments and changes to this ticket
-
Conrad Taylor March 11th, 2010 @ 11:45 PM
I was able to resolve this issue by doing the following:
... # GET /products/new # GET /products/new.xml def new @store = Product.new( :name => '', :price => 0.0 ) respond_to do |format| format.html # new.html.erb format.xml { render :xml => @product } end end ...
However, should it be really necessary to declare which arguments you're going to use?
-Conrad
-
Conrad Taylor March 11th, 2010 @ 11:47 PM
The above line should be as follows:
@product = Product.new( :name => '', :price => 0.0 )
-
Matthew Parker March 13th, 2010 @ 10:55 PM
- Tag changed from 2.3.5, activeresource to 2, activeresource
i created a gem called "fortify" after running into this exact problem: http://github.com/moonmaster9000/fortify
-
Gaël Deest March 18th, 2010 @ 02:25 PM
- Tag changed from 2, activeresource to 2, activeresource, patch
Just made a patch to fix this. This patch makes it possible to use new active resources with form_for, using:
Product.build
instead of:
Product.new
Hope this helps.
-
Gaël Deest March 18th, 2010 @ 02:28 PM
- Tag changed from 2, activeresource, patch to 2, 3.0.0.beta, activeresource, patch
-
Repository March 27th, 2010 @ 08:40 AM
- State changed from new to resolved
(from [753304bd113d7524f6d48d220483a2723c534557]) Makes ActiveResource work with form_for: - Adds a
build' method that can be used instead of
new' to load a new, unsaved resource from the remote site, filled with the correct default values. - Adds apersisted?' method that simply returns the opposite value than the
new?' method. [#4222 state:resolved] [#4155 state:resolved]Signed-off-by: wycats wycats@gmail.com
http://github.com/rails/rails/commit/753304bd113d7524f6d48d220483a2... -
Andrea Campi October 16th, 2010 @ 11:38 PM
- Tag changed from 2, 3.0.0.beta, activeresource, patch to 2-3-stable, 3.0.0.beta, activeresource, patch
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 4222 Using form_for with ActiveResource broken in edge After submitting a patch for ticket #4155 I realized that...
- 4222 Using form_for with ActiveResource broken in edge Here is a cumulated patch for this issue and the one rais...
- 4222 Using form_for with ActiveResource broken in edge (from [753304bd113d7524f6d48d220483a2723c534557]) Makes A...
- 4155 ActiveResource generates an error for a form_for attribute (from [753304bd113d7524f6d48d220483a2723c534557]) Makes A...