11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ルーティングをネストさせた場合のdeviseでのユーザー登録

Posted at

なんだか二日くらい悩んでいたのが解決した(っぽい)のでメモ。

routes.rb
 resources :communities do
    resources :rooms
    resources :users
  end

ユーザー登録は各コミュニティごとにさせたいので、上のようにroutes.rbを設定した。これだけでは、deviseのregistration_pathは

/users/registration

のままなので、どうにかする。

まず、ユーザー登録をカスタムコントローラーにやらせる。

app/controllers/registrations_controller.rb
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource   #ここを後から変更

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

end

を作成。その上で、routes.rbに

routes.rb
  devise_for :users, :controllers => {:registrations => "registrations"} do
    match "communities/:community_id/sign_up" => "registrations#new", :as => :new_community_user_registration
  end

と書いてdeviseにコントローラとルートをしらせてやる。

このままではviewがないので、

app/views/registrations/new.html.erb

を作成してやる。

にアクセスすると、ユーザー登録画面が表示される。ここまでで一段落。やれやれ

11
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?