Rails Devise 在注册尝试时重定向以登录

东切

在我尝试注册新用户后,Rails 现在重定向我以登录,您只会收到错误“您需要登录或注册才能继续”。新用户不会被保存到数据库中。

我的第一个想法是我正在重定向到需要身份验证的页面,但它不应该仍然创建一个新用户吗?我已经检查了我所有的控制器,唯一需要身份验证的控制器在创建、更新和销毁时都有。我有before_action :authenticate_user!, except: [:index, :show]两个控制器。这让我想到我的下一点,即身份验证不重要,因为我制作了一个注册控制器,在注册后重定向到主页(这是一个不需要身份验证的索引)。

注册控制器:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    root_path
  end
end

我的路线.rb:

Rails.application.routes.draw do
  root to: "home#index"
  devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }, 
    controllers: { registrations: "registrations" }

  resources :blogs do
    member do
      put 'like', to: 'blogs#upvote'
      put 'dislike', to: 'blogs#downvote'
    end
    resources :comments do
      member do
        put 'like', to: 'comments#upvote'
        put 'dislike', to: 'comments#downvote'
      end
    end
  end

  get 'tags/:tag', to: 'tags#show', as: :tag

  match '/users', to: 'users#index', via: 'get'
  match '/users/:id', to: 'users#show', via: 'get'

  #devise_for :users, :path_prefix => 'd'
  resources :users, :only =>[:show]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我关注所有列入白名单的内容:

module DeviseWhitelist 
  extend ActiveSupport::Concern

  included do
    before_filter :configure_permitted_parameters, if: :devise_controller?
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username, :picture, :picture_cache, :remove_picture])
  end
end

我的应用程序控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include DeviseWhitelist
  include DefaultPageContent
end

还有我的用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  mount_uploader :picture, PictureUploader
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates_presence_of :username
  validates_presence_of :picture
  validates_integrity_of :picture
  validates_processing_of :picture

  has_many :blogs
  has_many :comments
  has_many :logs
  acts_as_voter

  private
    def picture_size_validation
      errors[:picture] << "should be less than 500KB" if picture.size > 0.5.megabytes
    end
end

还有我的注册表:

<h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, multipart: true }) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %><br />
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: true %>
    <%= f.label :username %><br />
    <%= f.text_field :username %><br />

    <%= f.label :picture do %>
    <%= f.file_field :picture %>
    <%= f.hidden_field :picture_cache %>
    <% end %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Register" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

用户控制器:

class UsersController < ApplicationController

  def index
    @page_title = "Users"
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @page_title = @user.username
    @blogs = @user.blogs
    @articles = @user.articles
    @logs = @user.logs
    @almost_everything = (@blogs + @articles).sort{|b,a| a.updated_at <=> b.updated_at }
    @everything = (@almost_everything + @logs).sort{|b,a| a.updated_at <=> b.updated_at }
  end

end

这是我尝试注册新用户的控制台结果:

Started PUT "/" for 70.59.200.105 at 2017-05-13 16:12:06 +0000
Cannot render console from 70.59.200.105! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"uWuUhNMZzypqEDooxadjUf0/pIAXOJdmBuaynRSNvuqCSsx40FBPCOxt0howIiX9jLChlDsCmgmAtmGaqIMOzA==", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "username"=>"dave", "picture"=>#<ActionDispatch::Http::UploadedFile:0x007f38e4a702a0 @tempfile=#<Tempfile:/tmp/RackMultipart20170513-3045-wtfbgh.jpg>, @original_filename="42_ac79fe347c2c43c183e6d915b4922091.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[picture]\"; filename=\"42_ac79fe347c2c43c183e6d915b4922091.jpg\"\r\nContent-Type: image/jpeg\r\n">, "picture_cache"=>""}, "commit"=>"Register"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

Started GET "/login" for 70.59.200.105 at 2017-05-13 16:12:06 +0000
Cannot render console from 70.59.200.105! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Devise::SessionsController#new as HTML
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (2.1ms)
  Rendered devise/sessions/new.html.erb within layouts/application (19.8ms)
  Rendered shared/_nav.html.erb (5.5ms)
  Rendered shared/_footer.html.erb (8.0ms)
Completed 200 OK in 129ms (Views: 126.7ms | ActiveRecord: 0.0ms)

我觉得我已经检查了项目中的每个文件。为什么会发生这种情况?

东切

我通过更改在我的新注册视图中发布的方法解决了这个问题,但我还必须将其添加到我的注册控制器中:

class RegistrationsController < Devise::RegistrationsController
  protected

  wrap_parameters :user, include: [:username, :email, :password, :password_confirmation, :picture]

  def after_sign_up_path_for(resource)
    root_path
  end
end

看法:

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :post, multipart: true }) do |f| %>
  <%= f.error_notification %>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails-Devise-登录后防止重定向

来自分类Dev

Rails Devise禁用Ajax请求的重定向

来自分类Dev

Rails Devise Bootstrap注册模式

来自分类Dev

Rails Devise Bootstrap注册模式

来自分类Dev

Rails挂在Devise登录上

来自分类Dev

Rails Devise登录时添加用户

来自分类Dev

Rails Devise在登录时添加用户

来自分类Dev

Rails 4.0 Devise-多态-注册后重定向到用户

来自分类Dev

Rails Devise Twitter OAuth重定向到表单以继续进行oAuth注册

来自分类Dev

Rails 4.0 Devise-多态-注册后重定向到用户

来自分类Dev

注册后重定向并使用2个模型登录(Devise)

来自分类Dev

Rails 4 devise用户可以注册但不能登录,

来自分类Dev

Rails / Devise:在用户会话销毁时强制注销并重定向所有页面

来自分类Dev

Rails / Devise(设计令牌身份验证)在解锁帐户时重定向

来自分类Dev

Ruby on Rails Devise编辑注册路由错误

来自分类Dev

Devise Rails Bootstrap-注册模式

来自分类Dev

Rails Devise检查用户是否尚未登录

来自分类Dev

Rails,Devise多个模型登录及其会话

来自分类Dev

Devise-Rails 4-无法登录

来自分类Dev

Rails挂在Devise登录名上

来自分类Dev

Rails / Devise:自动登录后执行操作

来自分类Dev

Rails,Devise多个模型登录及其会话

来自分类Dev

直接在devise rails中登录

来自分类Dev

Rails:无法使用Devise登录用户

来自分类Dev

尝试注册现有用户时登录-Devise

来自分类Dev

Rails + Devise:如何检查登录时是否记住了用户

来自分类Dev

使用devise在Rails 3中发送密码重置指令后,如何将用户重定向到登录页面

来自分类Dev

登录后重定向时 Rails NoMethodError

来自分类Dev

Devise + Omniauth Facebook重定向到注册

Related 相关文章

热门标签

归档