small medium large xlarge

For facing_pragsmall
15 Sep 2012, 10:26
Vasili Chyrvon (8 posts)

Hi there! I do the task F (Add a dash of AJAX), and have a problem with my way of solving it…

In my line_items_controller.rb:

...
  def decrement
    @line_item = LineItem.find(params[:id])
    @line_item.quantity -= 1

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
      else
        format.html { redirect_to action: :destroy }
      end
    end
  end
...

My routes.rb is:

...
  resources :line_items do 
    post :decrement, on: :member
  end
...

I want do @redirect_to@ to the @destroy@ action in the same controller(i have validation of quality), but it redirects to the show action instead:

...
Started POST "/line_items/313/decrement" for 127.0.0.1 at 2012-09-14 21:55:41 +0400
Processing by LineItemsController#decrement as HTML
  Parameters: {"authenticity_token"=>"53cpUVyTWcKhZXJzqa2MbZYCbbLUyhBEVCk7cZMZ/lY=", "id"=>"313"}
  LineItem Load (0.0ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1  [["id", "313"]]
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
Redirected to http://localhost:3000/line_items/313
Completed 302 Found in 16ms (ActiveRecord: 0.0ms)


Started GET "/line_items/313" for 127.0.0.1 at 2012-09-14 21:55:41 +0400
Processing by LineItemsController#show as HTML
  Parameters: {"id"=>"313"}
  LineItem Load (0.0ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1  [["id", "313"]]
  Rendered line_items/show.html.erb within layouts/application (0.0ms)
Completed 200 OK in 62ms (Views: 62.5ms | ActiveRecord: 0.0ms)
...

I thought that should work, but it don’t. What I do wrong?

You must be logged in to comment