Reputation:
I am new to Rails and I need to create a simple Rails project with these conditions:
I used scaffold to generate a controller for articles and the gem Devise to create the authentication system. But I dont know how to implement the necessary conditions.
Thanks for the reply.
Upvotes: 1
Views: 1218
Reputation: 2532
If your user model is called user
, then you would include the following in your controller:
before_filter :authenticate_user!
If it not called user
, you would replace the word user in authenticate_user
with whatever it is.
You would add this directly under your controller declaration, like so:
class ArticlesController < ApplicationController
before_filter :authenticate_user!
#rest of code
end
If you want to restrict only certain actions in the controller to logged in users, you can use except
to exclude some actions. Here, index and show can be seen by anyone:
before_filter :authenticate_user!, :except => [:index, :show]
or only
to include specific actions. Here, only authenticated users can do the listed actions:
before_filter :authenticate_user!,
:only => [:new, :edit, :create, :update, :delete]
Upvotes: 5