mjc
mjc

Reputation: 3426

Rails nested resource with respond_with destroy action

What is the appropriate respond_with line for a nested resources destroy action?

My routes:

resources :vendors do
  resources :products, :except => [:index]
end

Product#destroy (note @vendor and @product are found with a before_filter which is omitted here)

def destroy
  @product.destroy 
  respond_with @vendor, @product
end

According to my functional tests, this is returning /vendors/X/products/X and not /vendors/X

Should I change it to just responed_to @vendor?

Upvotes: 3

Views: 2525

Answers (2)

fl00r
fl00r

Reputation: 83680

I believe Rails is smart enough to understand what to do if @product is destroyed

respond_with [@vendor, @product]

if not, then try this

respond_with @product, :location => vendor_path(@vendor)

Upvotes: 5

user668531
user668531

Reputation:

Sorry, that answer was completely wrong (misunderstood your problem):

Your destroy code can be like this:

def destroy
  @product = Product.find(params[:id])
  @product.destroy 
  redirect_to <route method for vendor's products index>, :notice => 'Any message'
end

See the exact route typing rake routes in your terminal.

params[:vendor_id] should be also available.

Upvotes: 2

Related Questions