时间:2016-05-02 18:14:22

标签: ruby-on-rails devise

我有一个复选框(比如使用条款),每次用户登录时我都需要进行检查。

我已经看到一些关于在注册页面上添加复选框,向用户模型添加虚拟属性等的示例。

= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
  %p
    = f.label :username, 'Username'
    = f.text_field :username
  %p
    = f.label :password
    = f.password_field :password
  %p
    %span
      = check_box_tag :terms_of_use
      I have read the
      = link_to 'Terms of Use', '#'
  %p
    = f.submit 'Sign in'

这是我的设计路线:

devise_for :users, controllers: { sessions: 'sessions' }

这是自定义控制器:

class SessionsController < Devise::SessionsController
  def create
    if params[:terms_of_use]
      super
    else
      # Not sure what to put here? Is this even the right track?
      # Also, redirect the user back to the sign in page and let
      # them know they must agree to the terms of use.
    end
  end
end

每次用户登录时,如何选中复选框?

1 个答案:

答案 0 :(得分:1)

此博文可能有所帮助:http://hollandaiseparty.com/order-of-abstractcontrollercallbacks/

添加prepend_before_action应该允许您在允许Devise接管之前检查terms_of_use并在需要时重定向。类似的东西:

class SessionsController < Devise::SessionsController

  prepend_before_action :check_terms_of_use, only: [:create]

  def check_terms_of_use
    unless params[:terms_of_use]
      # Since it's before the session creation, root_path will take you back to login
      redirect_to root_path
    end
  end
end