ActiveResource : how to create new instances


#1

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!