LoginSignup
10
11

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-03-18

長くなったので続き。
ここまででユーザー登録画面が表示されたけど、まだ所属コミュニティをユーザーに登録できない。
registration_contorollerのカスタマイズにちょっとはまったのでメモ。

routes.rbを修正

routes.rb
  devise_for :users, :controllers => {:registrations => "registrations"} do
    get "communities/:community_id/users/sign_up" => "registrations#new", :as => :new_community_user_registration
#次を追加
    post "communities/:community_id/users" => "registrations#create", :as => :community_user_registration
  end

で、form_forを編集

app/views/registrations/new.html.erb
<%= form_for(resource, :as => resource_name, :url => community_user_registration_path(@community)) do |f| %>

urlを修正して、パラメータに@communityを渡してやる。
createアクションを修正。

app/controllers/registrations_controller.rb
  #追加
  def new
    @community = Community.find(params[:community_id])
    super
  end

  def create
    #追加
    params[:user][:community_id] = params[:community_id]
    resource = build_resource(params[:user])

    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

これでなんとかコミュニティごとにユーザー登録できるようになった…と思う。
長かった。。。

10
11
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
10
11