redirect_to, array and the splat operator

Posted on

I was wondering if there were a way to make this controller code more readable:

events_controller.rb

format.zip do
  if @event.zip_cache_up_to_date?
    return [@event.photo_zip.file.url]
  else
    Resque.enqueue(PhotoZipper, @event.id, @event.class.to_s, current_user.id)
    return [events_url, notice: I18n.t('flash.photo_zip.create')]
  end
end

I decided to make a private method on the controller that will decide which path redirect_to should take, but it wasn’t working, because redirect_to params need to be separated by comma.

Exception

NoMethodError in EventsController#show 
undefined method `model_name' for Hash:Class

Code

format.zip { redirect_to zip_download_path }

private
def zip_download_path
  if @event.zip_cache_up_to_date?
    return [@event.photo_zip.file.url]
  else
    Resque.enqueue(PhotoZipper, @event.id, @event.class.to_s, current_user.id)
    return [events_url, notice: I18n.t('flash.photo_zip.create')]
  end
end

Then I found out about the *, the splat operator (http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/). Now it works!

Correction

format.zip { redirect_to *zip_download_path }

Leave a Reply

Your email address will not be published.