我已經創建了一個表單供用戶註冊我的應用程序,並通過添加能夠包含其公司名稱的小調整來註冊。但是,我的rspec測試並通過手動測試顯示參數未被傳遞以創建用戶/公司。Rails控制器/設計請參閱空參數
最後,我需要能夠創建一個user
,並創建一個company
,現在只需要公司名稱。
這裏有一個視覺的註冊形式:
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
屬性有關,但仍然無法解決這個謎團。
日誌中的錯誤說你有一個用戶已經與該電子郵件的存在。嘗試不同的電子郵件,並檢查 – Pavan
@Pavan我認爲這是一個設計std錯誤消息回滾。用戶由電子郵件地址創建,並且由於某種原因沒有電子郵件被傳遞。我在手動嘗試時看到的實際HTML輸出在功能測試中更具相關性。 – Godzilla74
@Pavan在這個查詢中,他選擇的電子郵件地址等於空白字符串'WHERE「users」。「」email「= $ 1 LIMIT $ 2 [[」email「,」「],'用戶'他實際上在參數'用戶[:電子郵件] =測試@ test.com'所以我想看到控制器行動 因爲這是設計,我不認爲你確實改變了創建行動,也許你應該調試創建操作,以瞭解請求有什麼問題。 –