Tutorial :Using RESTful Rails, How to Do an Insert and Create in One Action


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?


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?


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)  


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

And then add objects to container

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »