Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource generates an error for a form_for attribute

#4155

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

Reported by Conrad Taylor · March 11th, 2010 @ 10:56 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Conrad Taylor
    Conrad Taylor

    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

    March 11th, 2010 @ 11:45 PM

  2. Conrad Taylor
    Conrad Taylor

    The above line should be as follows:

    @product = Product.new( :name => '', :price => 0.0 )
    

    March 11th, 2010 @ 11:47 PM

  3. Matthew Parker
    Matthew Parker
    • 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

    March 13th, 2010 @ 10:55 PM

  4. Gaël Deest
    Gaël Deest
    • 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.

    March 18th, 2010 @ 02:25 PM

  5. Gaël Deest
    Gaël Deest
    • Tag changed from 2, activeresource, patch to 2, 3.0.0.beta, activeresource, patch

    March 18th, 2010 @ 02:28 PM

  6. Repository
    Repository
    • State changed from new to resolved

    (from [753304bd113d7524f6d48d220483a2723c534557]) Makes ActiveResource work with form_for: - Adds a build' method that can be used instead ofnew' to load a new, unsaved resource from the remote site, filled with the correct default values. - Adds a persisted?' method that simply returns the opposite value than thenew?' method. [#4222 Using form_for with ActiveResource broken in edge state:resolved] [#4155 ActiveResource generates an error for a form_for attribute state:resolved]

    Signed-off-by: wycats wycats@gmail.com
    http://github.com/rails/rails/commit/753304bd113d7524f6d48d220483a2...

    March 27th, 2010 @ 08:40 AM

  7. Andrea Campi
    Andrea Campi
    • Tag changed from 2, 3.0.0.beta, activeresource, patch to 2-3-stable, 3.0.0.beta, activeresource, patch

    October 16th, 2010 @ 11:38 PM