Quantcast
Channel: Ruby on Railsの記事一覧|TechRacho by BPS株式会社
Viewing all articles
Browse latest Browse all 1391

[Devise How-To]ユーザー登録ページへのルーティングをカスタマイズする(翻訳)

$
0
0

こんにちは、hachi8833です。Devise How-Toシリーズ、本日2本めです。

概要

原文の更新や誤りにお気づきの場合は、ぜひ@techrachoまでお知らせください。更新いたします。

[How-To] ユーザー登録ページへのルーティングをカスタマイズする(翻訳)

Customer::PrivateCustomer::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というパーシャル(部分テンプレート)があるとします。

Deviseの登録ルーティングを設定する

ルーティングファイル(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と記述すればビューで使えるようになります。

関連記事(Devise)


Viewing all articles
Browse latest Browse all 1391

Trending Articles