簡體   English   中英

用Devise跟蹤多個注冊路徑

[英]Rails multiple registration paths with Devise

我在Rails中使用devise,並嘗試創建第二個注冊路徑,用戶的默認注冊路徑(完美運行)和第二個注冊路徑,用戶可以通過以下操作在一個動作中注冊並創建項目: “入門”操作(想想Freelancer.com或非用戶可以通過一個操作注冊並創建他們的第一個項目的任何市場)。

我經歷了很多線程,但是它們似乎都想替換現有的注冊操作。 我想保留現有的注冊操作,並向現有的User::Registration控制器添加getting_started_newgetting_started_create動作,或者使用newcreate方法創建一個新的GettingStarted Controller,然后允許嵌套表格(針對用戶及其新項目)。

我最新的迭代看起來像這樣:

routes.rb

devise_for :users

resources :users do
  resources :projects
end

devise_scope :user do
  get "getting_started" =>'getting_started#new'
  post "getting_started" =>'getting_started#create'
end

Getting_started_controller.rb

class GettingStartedController < Devise::RegistrationsController

def new
  @user = User.new
  @user.projects.build  
end

def create
  @user = User.new(getting_started_params)
  redirect_to root, notice: "Done"
end

private
  def getting_started_params
    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

但是,當我嘗試並實際提交表單時,它將從新控制器加載get操作,並從devise注冊控制器加載post操作。

Started GET "/getting_started" for ::1 at 2016-10-17 09:15:58 +0200
Processing by GettingStartedController#new as HTML
Rendering getting_started/new.html.erb within layouts/application
Project_type Load (0.5ms)  SELECT "project_types".* FROM "project_types"
Industry Load (1.1ms)  SELECT "industries".* FROM "industries" ORDER BY "industries"."name" ASC
Feature Load (0.5ms)  SELECT "features".* FROM "features" ORDER BY "features"."name" ASC
Addon Load (0.4ms)  SELECT "addons".* FROM "addons"
Budget Load (0.4ms)  SELECT "budgets".* FROM "budgets"
Rendered getting_started/new.html.erb within layouts/application (32.9ms)
Rendered layouts/_header.html.erb (1.9ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 97ms (Views: 86.4ms | ActiveRecord: 2.9ms)


Started POST "/users" for ::1 at 2016-10-17 09:16:54 +0200
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hYrC/15zIF3DzPvhpFQH6LVS9H8XbhfHY1aRkgc2iX6Mdfvz33/O4p72XTpmY4X8E6B+dGfOmjZw7IoizvYwSA==", "user"=>{"first_name"=>"John", "last_name"=>"Smith", "phone"=>"0340239402309", "email"=>"test@example.com", "password"=>"[FILTERED]", "project"=>{"name"=>"This is a test", "description"=>"Tester Description", "feature_ids"=>[""], "addon_ids"=>[""]}}, "project"=>{"project_type_id"=>"4", "industry_id"=>"2", "budget_id"=>"1"}, "commit"=>"Create account"}
Unpermitted parameter: project
(0.1ms)  BEGIN
SQL (1.5ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "first_name", "last_name", "phone") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["email", "test@example.com"], ["encrypted_password", "$2a$11$VEVjSrnc5Y5c8ofXvNmlMOHrS.v4tQuBoKMLdHaSOA2S6fiVIHfE."], ["created_at", 2016-10-17 07:16:55 UTC], ["updated_at", 2016-10-17 07:16:55 UTC], ["first_name", "John"], ["last_name", "Smith"], ["phone", "0340239402309"]]
 (139.1ms)  COMMIT
 (0.1ms)  BEGIN
 SQL (0.4ms)  UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "users"."id" = $7  [["sign_in_count", 1], ["current_sign_in_at", 2016-10-17 07:16:55 UTC], ["last_sign_in_at", 2016-10-17 07:16:55 UTC], ["current_sign_in_ip", "::1/128"], ["last_sign_in_ip", "::1/128"], ["updated_at", 2016-10-17 07:16:55 UTC], ["id", 17]]
 (0.3ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 319ms (ActiveRecord: 141.7ms)

我意識到除了簡單地記錄用戶數據外,devise還完成了很多其他工作,因此最好將這些動作移至Devise User::RegistrationController ,然后調整可接受的參數。 但是,如何覆蓋Devise,以便它不會在您單擊“提交”按鈕的第二秒內自動使用devise create方法,而是使用getting_started_create操作?

首先, getting started是一個非常糟糕的控制器名稱,因為它不是名詞。 你不能說一個入門 因此,讓我們將其更改為QuickstartsController

讓我們為此資源設置路由:

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

每當您重寫庫控制器時,重要的是花一些時間研究您超類的類是如何實際工作的。

如果您看一下Devise控制器,幾乎所有操作都具有如下一行:

yield resource if block_given?

通過調用帶有塊的super ,您可以利用它來實現原始實現的流程:

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

但是,如何覆蓋Devise,以便它不會在您單擊“提交”按鈕的第二秒內自動使用devise創建方法,而是使用geting_started_create操作?

將表單指向正確的控制器-通過將url選項傳遞給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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM