2016-05-02 10 views
1

をパラメータを必要としている。は、私は、ユーザーのサインで毎回チェックする必要があります(使用条件など)のチェックボックスを持って工夫のサインインに毎回

私は、チェックボックスを追加することで、いくつかの例を見てきましたサインアップページで、Userモデルに仮想属性を追加するなど

= 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

このブログの記事は助けることがあります。http://hollandaiseparty.com/order-of-abstractcontrollercallbacks/

はprepend_before_actionを追加する工夫が引き継ぐことを許可する前に、あなたが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 
関連する問題