views:

53

answers:

1

I have a link on a website that says "add object". When I do this, an AJAX call is made and I want to do the following things:

1) if the container in the session does not exist, create one, else use existing 2) add the object to the container

I'm new to RESTful design and am wondering how to best accomplish this in Rails. Step #1 in particular.

When I make the AJAX call, what would the URI look like?

------edit------

I'm thinking the URI should be something like /myobject/new. Then, in a :before_filter, something like:

:before_filter check_for_container

def check_for_container
    if session[:container_id].nil?
        C = MyContainer.new
        session[:container_id] = C.id
    end
end

In my MyContainer controller, the new method has quite a bit of custom code to generate serial numbers, reuse lazy-deleted containers, etc. How can I refactor the existing code?

+1  A: 

What type of container? You can use something like:

 @container ||= []

if container - is simply array, or use next id container is record in database:

@container = Container.find_or_create(id)

or

@container = Container.find_or_create_by_field(:field=>id, :other_filed=>val....)

And then add objects to container

potapuff
see my edits...
Dex
replaceC = MyContainer.newbyC = MyContainer.create (new - create an object, but create object and record in db)You can use session object as container, if it will hold only small number of object ids, otherwise you can use in-memory key-value storage like memcahed - fast, and you can set TTL of container.
potapuff