views:

153

answers:

2

Hello,

I have installed and implemented the plugin restful_authentication from technoweenie.

My application is, for the most part, intended to be open, except for the case of say, writing a review.

So I have a reviews_controller.rb, but the only time I want to care whether the user is logged in or not is when they are submitting a specific action add_review.

add_review is an action on the vendor_controller.rb because the form is displayed on vendors/show. Then it redirects back to vendor/show to show the update (thinking of changing this to ajax).

If the person is NOT logged in, I want it to redirect to the login/signup page.

The instructions for restful_authentication show applying the include AuthenticatedSystem at the controller level, but I don't want to be authenticating with other actions in that controller.

How do I do this?

+4  A: 

Include the AuthenticatedSystem module in the controller you need it for (or ApplicationController if you need it for more than one), and then use the before_filter applied only to the actions you want, e.g.:

class VendorsController < ApplicationController
  include AuthenticatedSystem
  before_filter :login_required, :only => [:add_review]
end
AdminMyServer
before_filter can also take :except parameters if you want an action to NOT have the authentication.
Toby Hede
meaning, :except => [:non-authenticated-controller] ?
AFG
:except => [:non_authenticated_action]a before filter applies to all actions within a controller (and actions within controllers that subclass that controller). the :only/:except specify actions to make the filter more specific. use api.rubyonrails.org to find more information
AdminMyServer
A: 

I implore you to use the restful create action rather than your own add_review action. Read more about restful routing at: http://guides.rubyonrails.org/routing.html

Ryan Bigg
do you mean use the existing "create" ... I looked through the routing section...I wasn't clear what I should be looking for. Thanks.
AFG
Yes, I mean using the existing create. Having an add_review action in your ReviewsController is going against the norm and will be more painful than simply using the create action.
Ryan Bigg
So this may be dumb, but I am calling the add_review action from inside the vendors_controller.rb (a tutorial for similar functionality said to do so)...so I am not within the ReviewsController because the page is a view for Vendors.
AFG
Ok then, do nested restful routing. Have a look at the routing guide to see what I mean.
Ryan Bigg