devise 後增加選擇畫面


#1

JC大大,我想問一下
我User登入後,想加入一個畫面,選擇公司 A, B, C(必選)
我應該怎麼做?

想說一樣畫胡蘆,結果卡關
class ApplicationController < ActionController::Base
before_action :authenticate_user!
before_action :select_company!

def select_company!
redirect_to select_company_path # 掉入導向迴圈
end


#2

順一下

A登入 => B檢查是否有選 => C顯示選擇畫面 => D儲存 => E首頁

before_action :check_select_company
def check_select_company
  redirect_to select_company_path unless session[:company]
end

然後另外一邊

def select_company
  #show & post page
  if request.post?
    #檢查該使用者是否為該組織成員
    if CompanyUser.where(:user_id => current_user.id , :company_id => params[:id]).count > 0
      session[:company] = params[:id].to_i
      redirect_to index_path
    end
  end
  #get 或是 post 檢查不成功會直接到 view 去
end

route

match '/select_company' , :as => 'select_company' , :to => 'where#the_view' , :via => [:get , :post]

大概是這邏輯唄,以上