2017-06-29 17 views
0

我已經創建了一個表單供用戶註冊我的應用程序,並通過添加能夠包含其公司名稱的小調整來註冊。但是,我的rspec測試並通過手動測試顯示參數未被傳遞以創建用戶/公司。Rails控制器/設計請參閱空參數

最後,我需要能夠創建一個user,並創建一個company,現在只需要公司名稱。

這裏有一個視覺的註冊形式:

sign up form

RegistrationsController:

class Users::RegistrationsController < Devise::RegistrationsController 
    before_action :sign_up_params 

    def new 
    build_resource(sign_up_params) 
    self.resource.company = Company.new 
    respond_with self.resource 
    end 

    protected 

    def sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up) do |user| 
     user.permit(:email, :name, :password, :password_confirmation, company_attributes: [:name]) 
    end 
    end 
end 

型號Company.rb

class Company < ApplicationRecord 
    validates_uniqueness_of :domain, :stripe_id 
    validates_presence_of :name 
    has_many :users 
    has_many :devices 
    has_one :subscription 

end 

模型User.rb

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, :lockable, 
     :recoverable, :rememberable, :trackable, :validatable 
    validates_uniqueness_of :email 
    validates_presence_of :email, :name 
    belongs_to :company 
    accepts_nested_attributes_for :company 
end 

我的登記表:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 

    <%= f.fields_for :company do |company_form| %> 
     <%= company_form.text_field :name, class: 'form-control', autofocus: true, placeholder: 'ACME Inc.' %> 
    <% end %> 

    <%= f.text_field :name, class: 'form-control', placeholder: 'John Doe' %> 

    <%= f.text_field :email, class: 'form-control', placeholder: '[email protected]' %> 

    <%= f.password_field :password, class: 'form-control', autocomplete: 'off', placeholder: 'Password' %> 

    <%= f.password_field :password_confirmation, class: 'form-control', autocomplete: 'off', placeholder: 'Password Confirmation' %> 

    <%= f.submit 'Sign up', class: 'btn btn-primary btn-block btn-flat' %> 

<% end %> 

My功能測試(這是目前故障):

require 'rails_helper' 

RSpec.feature "User Registration", :type => :feature do 
    scenario "A new user is created" do 
    # this will cover the user profile signup info 
    visit "https://stackoverflow.com/users/sign_up" 
    fill_in "user_company_attributes_name", with: "ACME Inc." 
    fill_in "user_name", with: "Bob Jones" 
    fill_in "user_email", with: "[email protected]" 
    fill_in "user_password", with: "password" 
    fill_in "user_password_confirmation", with: "password" 
    click_button "Sign up" 
    expect(page).to have_text("You have signed up successfully") 
    end 
end 

的特徵測試結果:

Failures: 

    1) User Registration A new user is created 
    Failure/Error: expect(page).to have_text("You have signed up successfully") 
     expected to find text "You have signed up successfully" in "x 4 errors prohibited this user from being saved: Email can't be blankPassword can't be blankName can't be blankCompany must exist NeoUI Sign up to start getting secure can't be blank can't be blank can't be blank" 
    # ./spec/features/user_registration_spec.rb:25:in `block (2 levels) in <top (required)>' 

而只是爲了確認我試圖通過手工添加一個用戶:

website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Started POST "/users" for 172.19.0.1 at 2017-06-29 11:37:30 +0000 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Cannot render console from 172.19.0.1! Allowed networks: 172.23.0.0/172.23.0.255, 127.0.0.0/127.255.255.255, ::1 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Processing by Users::RegistrationsController#create as HTML 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiqRhDZ4hddR65vZ+EVPP3dezr3yemz5HuU0gy4Wo/Tt7xtpm00F8JG6kNdMjPWQiGrPOu9xk0FfF1HjtFaqiQ==", "user"=>{"company_attributes"=>{"name"=>"Test Company"}, "name"=>"Test User", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] (3.2ms) BEGIN 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] User Exists (1.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", ""], ["LIMIT", 1]] 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] (2.0ms) ROLLBACK 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Rendering /usr/local/bundle/gems/devise-4.3.0views/devise/registrations/new.html.erb within layouts/signup 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Rendered /usr/local/bundle/gems/devise-4.3.0views/devise/shared/_links.html.erb (4.1ms) [cache miss] 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Rendered /usr/local/bundle/gems/devise-4.3.0views/devise/registrations/new.html.erb within layouts/signup (35.3ms) 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] Completed 200 OK in 814ms (Views: 705.5ms | ActiveRecord: 7.1ms) 

我已經看過各種帖子e網絡試圖弄清楚這一點,並假設它與我在用戶表單中嵌套的company屬性有關,但仍然無法解決這個謎團。

+0

日誌中的錯誤說你有一個用戶已經與該電子郵件的存在。嘗試不同的電子郵件,並檢查 – Pavan

+0

@Pavan我認爲這是一個設計std錯誤消息回滾。用戶由電子郵件地址創建,並且由於某種原因沒有電子郵件被傳遞。我在手動嘗試時看到的實際HTML輸出在功能測試中更具相關性。 – Godzilla74

+0

@Pavan在這個查詢中,他選擇的電子郵件地址等於空白字符串'WHERE「users」。「」email「= $ 1 LIMIT $ 2 [[」email「,」「],'用戶'他實際上在參數'用戶[:電子郵件] =測試@ test.com'所以我想看到控制器行動 因爲這是設計,我不認爲你確實改變了創建行動,也許你應該調試創建操作,以瞭解請求有什麼問題。 –

回答

0

我認爲設計註冊控制器創建操作不使用強參數,並沒有得到該輸入。

他們出現在請求

"user"=>{"company_attributes"=>{"name"=>"Test Company"}, "name"=>"Test User", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 

但如果你讀你的查詢它與email = ""一個空字符串做很可能不允許作爲強大的參數,可以在事實。沒有參數正在被消毒。

WHERE "users"."email" = $1 LIMIT $2 [["email", ""], ["LIMIT", 1]] 
website_1 | [9493b284-c9dc-4832-9be9-e876feb621f4] (2.0ms) ROLLBACK 

也許你的sanitize方法寫錯了?也許你可以嘗試這樣,甚至包括他在應用程序控制器中......

def configure_permitted_parameters 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :name, company_attributes: [:name]]) 
end 

還與制定,我不包括在我的強烈PARAMS的:password,我看它是不是安全的,但現在我不知道。儘管我的理解標準Devise已經與:password:password_confirmation一起工作,但是當您創建新動作時,您可能需要所有參數...

另外,它可能是您的新動作在您的強參數控制器,但創造的行動,從這個控制器仍然使用自己的強勢PARAMS

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

+0

所以你基本上相信我正在處理這個問題,因爲'keys:'丟失了?當我回到辦公室時,我會給予'configure_permitted_pa​​rameters'這個小改動。 – Godzilla74

+0

作爲說明,它看起來並不像您可以更改設計使用的方法名稱。我原本有'sign_up_params',但只要按照建議將它改回到設計默認值,它就起作用了! – Godzilla74

+0

@ Godzilla74太棒了!是的,這不是很容易。設計是複雜的。我很高興這對你有所幫助。謝謝 –