Doubt Gem Devise

Asked

Viewed 175 times

0

Hello,

I’m using Gem Aim for a project of my own and a question has arisen, how do I leave only one route free, for example I want all other routes need login and password less the main page.

I did some research and arrived in this link but it didn’t work out.

Thank you

2 answers

1


How about using it this way?

Whereas you want to allow access without authentication only to the application’s main page:

application_controller.Rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  # Insira esta linha
  before_action :authenticate_user!
end

home_controller.Rb

class HomeController < ApplicationController
  # Insira esta linha
  skip_before_action :authenticate_user!

  def index
  end
end

This way, user authentication will be required in all controllers and their actions, except for controller responsible for the main page of your application.

And if for some reason you want to limit yourself to just the controller’s "index" action responsible for the main page of your application, you can do so:

skip_before_action :authenticate_user!, only: [:index]

I hope I’ve understood your problem and helped you solve it.

1

How about doing something like this ...

How you want to allow non-authenticated access only to the application’s main page:

That’s how I like to set up my app

1) in the route archive

  # config / routes.rb
  Rails.application.routes.draw do
    ide_for: users,: controllers => {registrações: 'users / registrations',
                                         sessões: 'usuários / sessões',
                                         senhas: 'usuários / senhas',
                                         confirmações: 'usuários / confirmações'
    }
    autenticar: o usuário faz
      namespace: os usuários fazem
        recursos: posts
        root: to => 'channels # index'
      fim
    fim
    recursos: contats
    match "/ about_us" => "pages # about_us",: as =>: about_us, via:: todos
    Páginas de raiz # índice '
  fim

thus, all routes that are outside ** namespace ** users are public

I also create a master user controller that extended application controller like this

# app / controllers / user_controller.rb
classe UserController <ApplicationController
  before_filter: authenticate_user!
fim

now for all device drivers

# app / controllers / users / registrations_controller.rb
Usuários da classe :: RegistrationsController <Devise :: RegistrationsController
  privado
  def after_sign_in_path_for (usuário)
    user_root_path
  fim
fim

My users' entire controller extends my user controller

# app / controllers / users / posts_controller.rb
classe Usuários :: PostsController <UserController
  ...
fim

You can follow this format for all other controllers. If you still need help, let me know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.