Complex Form Resource Controller

1 view
Skip to first unread message


Apr 12, 2009, 7:00:00 PM4/12/09
to resource_controller
I have a complex, multi-object form I am trying to implement using
Resource_Controller. As part of the create action, I would like to
save three object under one transaction. I currently save the primary
object, @object, using the framework and save the secondary objects,
@org and @employment, within the create.after block, however this is
not part of the @object transaction. My data model is somewhat
complex, so the @object table, Person, is indirectly associated to
employment and organization, so saving @object does not translate into
saving the other entities.

Is there a method I should implement in order to save all three at the
same time?

Thanks, Damon

James Golick

Apr 12, 2009, 7:38:04 PM4/12/09
resource_controller is really about keeping your controllers thin. Your models should contain the business logic required to save a bunch of objects.

You might want to have a look at active_presenter, which is for saving multiple models in one form.



Apr 12, 2009, 9:05:09 PM4/12/09
to resource_controller
I got it working using active_presenter, but decided to try using
Rails 2.3 nested object forms. The controller represents a simple view
of creating a customer, which has fields across several model
entities. It sounds like I shouldn't use r_c for this controller. Is
that what you're suggesting?

James Golick

Apr 12, 2009, 9:13:31 PM4/12/09
No, you should, but r_c should be working against a single model, rather than a bunch of calls to various models.

Mathieu Rousseau

Apr 28, 2009, 12:09:19 AM4/28/09

I have a user resource and a task resource. a task belongs to a user and user has many tasks.
I'm using rspec and resource_controller created a bunch of tests automatically.
My route looks like this:
map.resources :users, :has_many => :tasks

And so the tests in my tasks_routing_spec.rb broke. I changed them FROM :
    it "should map { :controller => 'tasks', :action => 'show', :id => '1'} to /tasks/1" do
      route_for(:controller => "tasks", :action => "show", :id => "1").should == "/tasks/1"


    it "should map { :controller => 'tasks', :action => 'show', :id => '1', :user_id => '1'} to /users/1/tasks/1" do
      route_for(:controller => "tasks", :action => "show", :id => "1", :user_id => "1").should == "/users/1/tasks/1"

I'm doing this manually and I'd like to know if there is a "better"/"automatic" way to do this?


Reply all
Reply to author
0 new messages