我有一个只有rails-api的app,我有两个型号。 我正在使用devise_auth_token进行身份验证
class User < ActiveRecord::Base
# attr_accessible :accounts_attributes
has_many :accounts
accepts_nested_attributes_for :accounts
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
include DeviseTokenAuth::Concerns::User
#Deleted ":confirmable," from above
after_create :assign_member_role
private
def assign_member_role
mr = MemberRole.first # default MR
Member.create!(user_id: self.id, member_role_id: mr.id)
end
def populate_account
end
end
帐户模型
class Account < ActiveRecord::Base
belongs_to :user
end
我的注册控制器如下: -
class RegistrationsController < DeviseTokenAuth::RegistrationsController
before_filter :sign_up_params
def new
@user = User.new
@user.accounts.build
# @user.accounts.build.links.build #-> creates one link object you can populate
end
def create
puts "In create"
params.inspect
super
end
private
def sign_up_params
# params.require(:user).permit!(:account_atrributes)
puts "I am called in sign_up method"
devise_parameter_sanitizer.for(:sign_up) { |u|
# u.permit(:email, :password, :password_confirmation, :accounts_attributes => :school_name)
u.permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
}
params.permit!
end
end
我在终端中收到如下未知属性错误。
Started POST "/auth?email=email112@meail.com&password=[FILTERED]&password_confirmation=[FILTERED]&accounts[:school_name]=mjdhs&accounts[:line_1]=asdff&accounts[:line_2]=ijdkjd&accounts[:city]=mum&accounts[:state]=mah&accounts[:country]=IN&accounts[:pincode]=400054" for 127.0.0.1 at 2015-10-07 12:50:31 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by RegistrationsController#create as */*
Parameters: {"email"=>"email112@meail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}}
I am called in sign_up method
I am called in sign_up method
In create
I am called in sign_up method
Completed 500 Internal Server Error in 106ms (ActiveRecord: 0.9ms)
ActiveRecord::UnknownAttributeError (unknown attribute 'controller' for User.):
app/controllers/registrations_controller.rb:24:in `create'
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.4ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (19.7ms)
我正在尝试使用json中的postman创建一个新用户。我想在每个用户创建时完成填充帐户模型。
如果我在注册控制器中使用以下代码。
before_filter :user_params
puts "hello"
GET /users/sign_up
def new
# Override Devise default behaviour and create a profile as well
build_resource({})
resource.build_account
respond_with self.resource
end
def create
puts "\n\n Testing if the create method can be overriden\n\n"
puts params.inspect
params.require(:users).permit!
super
end
protected
def user_params
puts "in loop\n\n\n\n"
devise_parameter_sanitizer.for(:sign_up) { |u|
# u.permit(:email, :password, :password_confirmation, :accounts_attributes => :school_name)
u.permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
}
# params.require(:user).permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
end
end
我可以创建新用户,但无法填充帐户模型。并得到未知的params错误&amp;参数缺失错误如下所示。
Started POST "/auth?email=email112@meail.com&password=[FILTERED]&password_confirmation=[FILTERED]&accounts[:school_name]=mjdhs&accounts[:line_1]=asdff&accounts[:line_2]=ijdkjd&accounts[:city]=mum&accounts[:state]=mah&accounts[:country]=IN&accounts[:pincode]=400054" for 127.0.0.1 at 2015-10-07 13:01:33 +0530
hello
Processing by RegistrationsController#create as */*
Parameters: {"email"=>"email112@meail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}}
Unpermitted parameter: accounts
in loop
Testing if the create method can be overriden
{"email"=>"email112@meail.com", "password"=>"password", "password_confirmation"=>"password", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}, "controller"=>"registrations", "action"=>"create"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: users):
app/controllers/registrations_controller.rb:65:in `create'
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.5ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (17.3ms)