我的申请存在一个小问题。
不是让我的访问者选择登录,而是重定向访问我的应用程序的所有人立即登录,或者他们无法浏览任何其他标签。
为了进一步澄清我的application.html.erb文件,我有以下代码:
<% if user_signed_in? %>
Logged in as <strong><%= current_user.email %></strong>
<br>
<br>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %> </p>
<% end %>
如果有人可以协助我指明我需要采取哪条路线作为选项登录,而不是作为我申请的所有访问者的默认路线,我们将不胜感激。
在我的application_controller.rb文件中,代码如下:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!
end
答案 0 :(得分:3)
在您不想强制进行身份验证的控制器中,添加skip_filter
声明:
skip_filter :authenticate_user!
如果您有一个控制器,其中只有一些操作应该是非认证的(例如索引和显示操作):
skip_filter :authenticate_user!, only: [:index, :show]
或者您可以将所有操作都打开,但某些操作除外:
skip_filter :authenticate_user!, except: [:create, :update]