设计添加第二组嵌套路由

时间:2015-07-31 17:47:29

标签: ruby-on-rails ruby-on-rails-4 devise routing

我正在使用Devise 3运行Rails 4应用程序,我想为Devise设置一组辅助路由。我有一个Portal模型,它代表一个独特的子站点,但共享相同的核心用户群。我希望能够将用户发送到/portals/:portal_id/users/sign_in之类的路由,并使其行为与Devise使用/users/sign_in创建的默认devise_for :users路由的行为相同。我怎么能这样做?

以下是我的路线:

MyApp::Application.routes.draw do
    devise_for :users, :controllers => {:registrations => "registrations"}
    root 'general#home'

    resources :portals, controller: 'portals/general' do
        member do
            root 'portals/general#home'
        end
    end
end

我还有RegistrationsController覆盖默认的Devise控制器,如下所示:

class Portals::RegistrationsController < RegistrationsController
    layout 'portals/layouts/application'

    def create
        @portal = Portal.friendly.find(params[:id])
        build_resource(sign_up_params)

        resource_saved = resource.save
        yield resource if block_given?
        if resource_saved
            if resource.active_for_authentication?
                set_flash_message :notice, :signed_up if is_flashing_format?
                sign_up(resource_name, resource)
                redirect_to :back
            else
                set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
                expire_data_after_sign_in!
                respond_with resource, location: after_inactive_sign_up_path_for(resource)
            end
        else
            clean_up_passwords resource
            # respond_with(resource, location: root_path)
            flash[:notice] = resource.errors.full_messages.first
            redirect_to root_portal_path(resource)
        end
    end
end

0 个答案:

没有答案