- Ruby / Rails関連
READ MORE
こんにちは、hachi8833です。Devise How-Toシリーズ、本日2本めです。
原文の更新や誤りにお気づきの場合は、ぜひ@techrachoまでお知らせください。更新いたします。
Customer::Private
とCustomer::Public
という2つのDeviseユーザーモデルがあるとします。
models/customer/private.rb
models/customer/public.rb
それぞれのユーザーのアクションについてスコープを設定するために、以下のコントローラを作成します。
# app/controllers/customer/private/registrations_controller.rb
class Customer
class Private
class RegistrationsController < Devise::RegistrationsController
end
end
end
以下のような2つのビューを作成する必要があります。
views/customer/private/registrations/new.html.haml
views/customer/public/registrations/new.html.haml
You will likely want to have a _form.html.haml partial for each.
それぞれのビューには_form.html.haml
というパーシャル(部分テンプレート)があるとします。
ルーティングファイル(conf/routes.rb)に以下を記述します。
devise_for :private_customers, :class_name => 'Customer::Private', :controllers => {:registrations => "customer/
private/registrations", :sessions => 'main' } do
get "private_customer/sign_up" => "customer/private/registrations#new", :as => :private_customer_signup
get "private_customer/sign_in" => "main#index", :as => :private_customer_signin
end
上はprivate_customers
用です。public_customers
についても同じように設定します。
あとはビューにlink_to 'register', private_customer_signup_path
と記述すればビューで使えるようになります。