0

I read in another answer that it's bad practice to redirect_to a different controller's create action.

I'm trying to implement a Create Foo and Bar button in my form on Foos#new.

Foo uses the information from the form and Bar can use Foo's id to Foo.find(params[:foo_id]) to get the information from Foo it needs. It makes sense to me that I should do something like:

if params['route_to']['bar']
  redirect_to controller: :bars, action: :create, foo_id: @foo.id
else
  redirect_to foos_path
end

at the end of my Foos#create.

  • Am I making an architectural mistake?
  • What is a better way?
  • How do I tell redirect_to to POST instead of GET? Because right now I'm submitting a GET when I want a POST
  • Should I use concerns?
Community
  • 1
  • 1
mbigras
  • 7,664
  • 11
  • 50
  • 111

1 Answers1

1

If I understand you correctly, you want to create a Bar object after creating a Foo object? I'd just do that in the create method for Foo.

Something like

def create
    @foo = Foo.new(foo_params)
    if @foo.save
      @bar = Bar.new
      @bar.attribute1 = @foo.attribute_bar_needs
      @bar.save
    end
end

Even better, if a bar object needs to be created after every Foo object, then I'd make an after_create callback in the Foo model to do so instead of doing it in a controller.

boboguitar
  • 66
  • 9