Forum: Ruby on Rails ActiveResource : how to create new instances

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7af83f177e36ed1edc9cca3c9817a4ff?d=identicon&s=25 Matt Bosworth (Guest)
on 2007-02-22 21:34
(Received via mailing list)
Hi all,

  I've been playing around with ActiveResource for about a week now, and
it's super-cool.

  That said, I'm having a little conceptual problem and I'm hoping
someone
smarter than me has already solved it.

If I have a model :

class FooModel < ActiveResource::Base
end

and a controller :

class FoosController < ApplicationController

def new
  @fm = FooModel.new
end
end

and a view :

<%= form_tag %>
<%= text_field 'fm', 'name' %>
<%= end_form_tag %>

I get a NoMethodError.  Which makes a lot of sense, since I can't figure
out
how ActiveResource could magically know what attributes live in
FooModel.
My question is, what is the right way to deal with this?  Add
attr_accessor
:name ?  Or something like this :

class FooModel < ActiveResource::Base

def blank
  fm = new
  fm.attributes = { :name => nil }
end
end

Or is there something cool that can be done with method_missing ?  Maybe
:

def method_missing(method_symbol, *arguments)
  super #want to call the method_missing in ActiveResource::Base
first...
  rescue NoMethodError
    attributes[method_symbol.to_s] => nil
    nil
end

Or perhaps the best thing to do is not use the form helpers and add the
attributes to the model object after the submit (in the controller)?

Any help or pointers would be greatly appreciated!
This topic is locked and can not be replied to.