Rails: How to use a before_filter to protect multiple methods

So, I have this code in a controller:

before_filter :require_login, :only => :new, :edit, :destroy

My controller has these methods: index, new, edit, create, update, show, destroy. What I want to do is to protect with login_required (:require_login in the code) the methods: new, edit, destroy, but the above code doesn't work, I can protect one method if i have, for example:

before_filter :require_login, :only => :new

But I want to protect the three of them, How can I do it?


You're missing square brackets around the only option's value:

 before_filter :require_login, :only => [:new, :edit, :destroy]

It's not working because the Ruby interpreter doesn't know where the options for only start and the arguments for before_filter continue. This is case where you need to be explicit about the container.

Use an array:

before_filter :require_login, :only => [:new, :edit, :destroy]

Oh I was a FOOL!!! it was just matter of putting them in array form -.- like this:

before_filter :require_login, :only => [:new, :edit, :destroy]

Sorry for the obvious question.

Need Your Help

Returning 404 in Angular

angularjs model-view-controller http-status-code-404

I'm familiar with returning 404 as the HTTP status code when you visit pages that do not exist. When you build static web sites, it works well and it makes sense, but in Angular it's different.

RPM package spec localization

linux fedora rpm rpm-spec

I need to package proprietary program for Russian local market.