有条件注册rails 4



我的应用程序有一个小问题。

它没有给我的访问者登录的选项,而是重定向所有访问我的应用程序的人立即登录,否则他们将无法浏览任何其他选项卡。

为了在我的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

在不想强制进行身份验证的控制器中,添加skip_filter声明:

skip_filter :authenticate_user!

如果您有一个控制器,其中只有一些操作应该是未经身份验证的(例如索引和显示操作):

skip_filter :authenticate_user!, only: [:index, :show]

或者,您可以打开除某些操作之外的所有操作:

skip_filter :authenticate_user!, except: [:create, :update]

最新更新