Instance vs local variables in controller method when they wont be used by a view

In the destroy method of our DecksController we use an instance variable to retrieve the deck we have to delete and then destroy it:

  def destroy
    @deck = Deck.find(params[:id]) 
    redirect_to "/decks"

I understand the need to assign the deck to an instance variable in, say, our show method, because our show view will use that variable, but i don’t really see the need for doing that when no view will use it.

Is there a particular reason to using an instance variable in destroy even if no view will use that variable? Or is it just considered best practice?


I can’t think of a good reason to assign it to an instance variable if you don’t need it in the view.

I’d just use a regular local.

Oh, also, I’d change your last line to redirect_to decks_path. If you hard-code the string like that, you’re likely duplicating something that would be better off defined in your routes.rb file.

Great, thanks for clearing that up, and thanks for the tip.

I believe he was using:
redirect_to "/decks"
in his code because that is what Matt showed in his workshop… Matt starts with the raw basics in the beginning, then refactors and shows the best practices later… we hasn’t got there yet =)
well, now he did =)

Ahh. Makes sense. Carry on then!