我有一段代码可以更新人员信息。
module People
class Update
attr_reader :person_id, :params
def call person_id, params
@person_id = person_id
@params = params
update_person
end
def update_person
@person = Person.find(person_id)
@person.update(params)
@person
end
end
end
params等于
{"first_name"=>"Blue", "last_name"=>"Panther", "email"=>"bluepanther9@gs.co.za"}
person_id有一个值并存在于数据库中。非常简单直接。
奇怪的是,当一切都通过应用程序执行时我得到一个错误,但是当我通过rspec或手动控制台执行它时它工作正常。见下面的错误。
ActiveModel::ForbiddenAttributesError
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activemodel-4.2.0.beta4/lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:249:in `block in update'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:345:in `block in with_transaction_returning_status'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:218:in `transaction'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:342:in `with_transaction_returning_status'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:248:in `update'
/storage/www/testing.phoenix.site.co.za/app/services/people/update.rb:14:in `update_person'
/storage/www/testing.phoenix.site.co.za/app/services/people/update.rb:9:in `call'
/storage/www/testing.phoenix.site.co.za/app/services/people/create_or_update_person.rb:24:in `create_or_update_person'
/storage/www/testing.phoenix.site.co.za/app/services/people/create_or_update_person.rb:15:in `call'
/storage/www/testing.phoenix.site.co.za/app/services/events/incoming/identify.rb:19:in `create_or_update_person'
/storage/www/testing.phoenix.site.co.za/app/services/events/incoming/identify.rb:13:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/performable_method.rb:30:in `perform'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/backend/base.rb:94:in `block in invoke_job'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `block in initialize'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `execute'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/backend/base.rb:91:in `invoke_job'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:199:in `block (2 levels) in run'
/home/getsmart/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:91:in `block in timeout'
/home/getsmart/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:101:in `call'
/home/getsmart/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:101:in `timeout'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:199:in `block in run'
/home/getsmart/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:198:in `run'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:275:in `block in reserve_and_run_one_job'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `block in initialize'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `execute'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:275:in `reserve_and_run_one_job'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:182:in `block in work_off'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:181:in `times'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:181:in `work_off'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:146:in `block (4 levels) in start'
/home/getsmart/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:145:in `block (3 levels) in start'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `block in initialize'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `execute'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:144:in `block (2 levels) in start'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:143:in `loop'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:143:in `block in start'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/plugins/clear_locks.rb:7:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:79:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:79:in `block (2 levels) in add'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:61:in `block in initialize'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:79:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:79:in `block in add'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:66:in `execute'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/worker.rb:142:in `start'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:124:in `run'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:112:in `block in run_process'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application.rb:255:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application.rb:255:in `block in start_proc'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:in `call_as_daemon'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application.rb:259:in `start_proc'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application.rb:296:in `start'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application_group.rb:159:in `block (2 levels) in start_all'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application_group.rb:158:in `fork'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application_group.rb:158:in `block in start_all'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application_group.rb:157:in `each'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/application_group.rb:157:in `start_all'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/controller.rb:80:in `run'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons.rb:197:in `block in run_proc'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `call'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `catch_exceptions'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/daemons-1.1.9/lib/daemons.rb:196:in `run_proc'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:110:in `run_process'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:91:in `block in daemonize'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:89:in `times'
/home/getsmart/.rvm/gems/ruby-2.1.2@phoenix/gems/delayed_job-4.0.4/lib/delayed/command.rb:89:in `daemonize' bin/delayed_job:5:in `<main>'
请注意,代码正在从延迟作业执行。不确定这是否会产生影响。
答案 0 :(得分:3)
您应该替换为
@person.update(params.permit(:first_name, :last_name, :email))
你不应盲目使用permit!
,重点是确保人们只能改变你允许的内容。