Rails redirection action: index, not working - ruby-on-rails

I'm looking for a solution.
I'm trying to redirect users when they are not an admin.
I've done that :
def is_admin?
if current_user.admin?
redirect_to action: "index"
else
redirect_to root_path
end
end
I have before_action :is_admin? in my posts_controller
I dunno why exactly, but the redirection not working. Firefox gives me a blank page with:
The page is not redirected correctly
Thanks for your help

The problem is your before_action :is_admin? which call is_admin? on index methods and keep redirecting on themself...
I don't understand why redirecting on 'index', you should instead change your method is_admin? like :
def is_admin?
# redirect ONLY if user isn't admin
redirect_to root_path unless current_user.admin?
end

Related

If i use redirect_to i'll get Redirected too much on browser

My goal is to redirect user to index path if true else show new path.
class PostsController < ApplicationController
before_action :check_condition, only: [:index, :new]
def check_condition
if true
redirect_to posts_path
else
redirect_to new_post_path
end
end
def index
#posts = Post.find()
end
def new
#new_post = Post.new(title: "test")
end
end
I keep getting error redirected too much on the browser when I go to index path or new path
At the moment, you redirect to post_path every time when the if-condition is true no matter if you are already on the post_path.
You only need to redirect unless you are already on the path to the expected method. That can be done by checking the current action_name:
def check_condition
if true
redirect_to posts_path unless action_name == 'index'
else
redirect_to new_post_path unless action_name == 'new'
end
end
You're running to an infinite loop of redirects.
You're adding check_condition as before_action. This means, every time you redirect to index or new pages, check_condition will run again without reaching index or new methods and redirecting again instead, and so on.
My suggestion is to write the condition checker in the view or in a helper method (to determine the path of the link in the first place), not in the controller.
Try this. You need to return after redirect.
def check_condition
if true
redirect_to posts_path && return
else
redirect_to new_post_path && return
end
end
Sorry but, what's the condition to check? If the user is logged in? I think your issue is probably too many redirect_to's. Try:
def check_condition
path = condition ? posts_path : new_post_path
redirect_to path
end

Setting up logging out...is there a way to check if a controller action contains a before_action

I have a before_action :logged_in_user in my controller which redirects to the login_path if there is no current_user.
I am struggling with the logic of how to setup logout (destroy a session) in my app.
If a user is on a page where they are not required to be logged_in?, I want the logout just to redirect_to :back (stay on that page) since it does not effect the current page viewing.
If they are on a page that requires that they are logged_in?, I want them to be redirect_to :root_url, because otherwise they will be redirect_to the login_path which is awkward since they just logged_out.
So basically in pseudo code I want to do the following:
redirect_to :back
unless :back controller:action >> before_action :logged_in_user
then redirect_to root_url
SessionsController
def destroy
destroy_location
log_out if logged_in?
redirect_logout
end
def destroy_location
path = ["/feed", "/friends", "/saved_articles", "/favorites"]
if path.any?{|word| URI(request.referrer).path == word }
session[:exit] = root_url
end
end
def redirect_logout
redirect_to(session[:exit] || :back)
session.delete(:exit)
end
This works rather nicely!

How to put Auth Allow in ruby like CakePHP?

I have a users_controller.rb. There are too many method including login, register, forgot_password and logout I want to put auth allow these actions in my ruby controller.
I have done $this->Auth->allow in the CakePHP.
$this->Auth->allow('register', 'login', 'forgot_password', 'logout');
But in the ruby this is very hard to put. Please suggest me -
def login
#title = 'Login'
#render layout: 'login'
end
def dashboard
if logged_in?
#title = 'My Dashboard'
#user = User.get_profile(session[:user_id])
#user = User.get_profile(session[:user_id])
#raise #myProfile.inspect
else
redirect_to '/login'
end
end
def my_profile
if logged_in?
#title = 'My Profile'
#user = User.get_profile(session[:user_id])
else
redirect_to '/login'
end
end
def logout
log_out
redirect_to '/login'
end
Each time I am adding if logged_in? ... else ... end in my every action. So
I want to put Auth Allow in ruby like CakePHP code. Please help me.
Those actions should be in separate controllers, there are plenty of resources available to explain this, search for "RESTful Rails".
Once they are in separate controllers you can use a "before" action to prevent unauthorised users from accessing those actions.
It looks like you've created your own authentication system, instead of using a gem, so if you want a method to check for a logged in user, you can add it.
In application_controller.rb
def authenticate_user
redirect_to login_path unless logged_in?
end
Then in any controller you want to require a user to be signed in you can do
class YourController < ApplicationContoller
before_action :authetnicate_user, except: [:actions_that_doesnt_need_auth]
...
# All normal methods
end
That being said - the previous answer about using RESTful resources is important to understand and keep in mind. If you have questions you can ask :)

How to not apply before_filter for root route in rails?

I have a before_filter called check_login that looks something like this:
def check_login
if not session[:user_id]
flash[:error] = "Please log in to continue"
redirect_to login_path
end
end
I then put this before_filter in my application controller, and then exclude it in my login controller (with skip_before_filter :check_login)
The problem is that when the user hits the homepage for the first time (i.e. just localhost:3000), it will redirect them to the login page with the flash[:error] message displaying. However, for the homepage, I just want to show the login form. What's the cleanest way to handle this 'special-case'? I thought about putting the skip_before_filter in the controller that handles the homepage, but I didn't think this was very DRY, since if I change the homepage in the routes file, I'll have to also change the location of the skip_before_filter.
Thanks!
You can add some action in your filter
class LoginController < ApplicationController
skip_before_filter :check_login, :only => [:login]
def login
end
end
And in Application Controller, "blank?" check on presence and nil. It useful
def check_login
if session[:user_id].blank?
flash[:error] = "Please log in to continue"
redirect_to login_path
end
end
You can add named action for your homepage:
class StaticPagesController < ApplicationController
def home
end
end
And then check the current action in your callback:
def check_login
if not session[:user_id]
flash[:error] = "Please log in to continue" unless params[:action] == "home"
redirect_to login_path
end
end

error: Too many redirects

I'm using devise and trying the next following:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :is_worker
def is_worker
if user_signed_in?
#email = current_user.email
if #email && Worker.find_by_email(#email).nil?
redirect_to '/tasksadmins'
else
redirect_to '/workers'
end
else
redirect_to '/users/sign_in'
end
end
end
when I try to enter the site: localhost:3000/tasksadmins, I got:
Oops! It was not possible to show this website
The website at http://localhost:3000/tasksadmins seems to be unavailable. The precise error was:
Too many redirects
It could be temporarily switched off or moved to a new address. Don't forget to check that your internet connection is working correctly.
How can I fix it please?
before_filter is applied to every single request. That's why it's redirecting again and again.
You might want to only filter specific actions:
before_filter :is_worker, only: :index
Another solution would be to check wether a redirect is necessary in #is_worker:
redirect_to '/workers' unless request.fullpath == '/workers'
EDIT:
Another way would be to skip the before filter for the target actions of your redirects. Example:
class WorkersController < ApplicationController
skip_before_filter :is_worker, only: :index
# …
end
In my case:
users_controller.rb
before_action :logged_in?, only: :new
def new
#user = User.new
render layout: "session"
end
and
application_controller.rb
def logged_in?
redirect_to users_new_url unless current_user.present?
end
When I was trying to redirect to the 'users/new' page,same error occurred.
This is just because I'm trying to redirect to the 'users/new' page and "def logged_in?" is also redirecting to the same page.
Then I changed the application_controller.rb code like this:
def logged_in?
redirect_to root_url unless current_user.blank?
end
Error_Resolved.

Resources