cancel default controller action's redirect

97 Views Asked by At

There's this link:

= link_to "Close", close_ticket_path(:id => ticket.id), :onclick => $(this).closest('tr').remove()", :remote => true

which routes to tickets_controller#close:

  def close
    @ticket  = Ticket.find(params[:id]).update_attribute(:status, "closed")
  end

By default, this action will redirect to views/tickets/close. How to disable this default redirect? I know how to specify custom redirect, but this is not what I need. The view is managed by the script and the db is modified in the action, so no more action is needed and I don't want the page to refresh. render won't work neither, as the content in question is loaded dynamically, so basically doing precisely nothing is what I need.

I found this workaround to e.g. include respond_to :js and create empty close.js.erb file, or to move above :onclick to this file, but I want to do it idiomatically. As a matter of fact, the link works now as desired with the respond_to :js in the action (not included here), but console throws Missing template error, which is not very nice. I looked in Rails Redirecting, in JS with Rails, Rendering guide, and some more, but to no avail.

2

There are 2 best solutions below

0
On BEST ANSWER

Actually, after reading this, I must do the justice and post the officially recommended way of handling this case. The action should read:

def close
  @ticket  = Ticket.find(params[:id]).update_attribute(:status, "closed")
  head :ok
end

Thank you, @auL5agoi, for leading me to this answer.

1
On

Try this:

def close
  @ticket  = Ticket.find(params[:id]).update_attribute(:status, "closed")
  render nothing: true, :status => 200
end