Rails multiple registration paths with Devise

﹥>﹥吖頭↗ 提交于 2019-12-03 21:08:35

First off getting started is a pretty horrible name for a controller since it is not a noun. You can't say a getting started. So lets change it to QuickstartsController.

Lets setup the routes for this resource:

Rails.application.routes.draw do

  # These are your "normal" devise routes
  devise_for :users

  # This is the mapping for the "quickstart" routes
  devise_for :quickstarts,
    class_name: 'User',
    only: [],
    controllers: { registrations: 'quickstarts' }

  # These are the actual routes for quickstarts
  devise_scope :quickstart do
    get   "/quickstarts/new", to: "quickstarts#new", as: :new_quickstart
    post  "/quickstarts",    to: "quickstarts#create", as: :quickstarts
  end
end

Whenever you are overriding a library controller it is important to take some time to study how the class you are superclassing actually works.

If you look at the Devise controllers almost all the actions have a line like:

yield resource if block_given?

Which lets you tap into the flow of the original implementation by calling super with a block:

class QuickstartsController < Devise::RegistrationsController

  # GET /quickstarts/new
  def new
    # This block is passed to the super class implementation:
    super do |resource|
      resource.projects.build
    end
  end

  # POST /quickstarts
  def create
    # You can pass a block here too if you want.
    super
  end

  private

  # This should redirect to the newly created project
  def after_sign_up_path_for(resource)
    polymorphic_path( resource.projects.last )
  end

  # This is the method Devise devise calls for the params.
  def sign_up_params
    # Don't write your params sanitiation on one line! Its not cool.
    params.require(:user)
          .permit(
              :first_name, :last_name, :phone, :password, :email,
              projects_attributes: [
                 :user_id, :project_type_id, :name,
                 :industry_id, :description, :budget_id,
                 :project_status_id,
                 feature_ids:[],
                 addon_ids:[]
              ]
          )
  end
end

But how do you override Devise so that it doesn't auto use the devise create method the second you click the submit button, and would instead use a getting_started_create action?

Point the form to the correct controller - by passing the url option to form_for.

app/views/quickstarts/new.html.erb:

<h2>Getting Started</h2>

<%= form_for( resource, as: :user, url: quickstarts_path ) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <fieldset>
    <legend>Project</legend>
    <% f.fields_for :projects do |pf| %>
      <%# ... %>
    <% end %>
  </fieldset>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!