SyntaxError - syntax error, unexpected '=', expecting keyword_end
方法将字符串转换为我的Rails应用程序中的对象时,我得到了send
。奇怪的是,当我使用它来比较价值时,它正在找工作,如下所示:
if @whiteboard.contract.send(user_ast_contract_accept) == true
....
end
但是,当我用它来更新值时,我得到了那个错误。以下是完整错误:
SyntaxError - syntax error, unexpected '=', expecting keyword_end
user.send(user_credit_check) = "Submitted"
^:
app/controllers/whiteboards_controller.rb:355:in `'
activesupport (4.2.3) lib/active_support/dependencies.rb:457:in `block in load_file'
activesupport (4.2.3) lib/active_support/dependencies.rb:647:in `new_constants_in'
activesupport (4.2.3) lib/active_support/dependencies.rb:456:in `load_file'
activesupport (4.2.3) lib/active_support/dependencies.rb:354:in `require_or_load'
activesupport (4.2.3) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.2.3) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `constantize'
activesupport (4.2.3) lib/active_support/dependencies.rb:566:in `get'
activesupport (4.2.3) lib/active_support/dependencies.rb:597:in `constantize'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:821:in `call'
以下是我的代码,包括我所做的功能:
def get_member_position(user, contract)
if user.id == contract.tenant1.to_i
return "tenant1"
elsif user.id == contract.tenant2.to_i
return "tenant2"
elsif user.id == contract.tenant3.to_i
return "tenant3"
elsif user.id == contract.tenant4.to_i
return "tenant4"
elsif user.id == contract.landlord.to_i
return "landlord"
elsif user.id == contract.guarantor1.to_i
return "guarantor1"
elsif user.id == contract.guarantor2.to_i
return "guarantor2"
elsif user.id == contract.guarantor3.to_i
return "guarantor3"
elsif user.id == contract.guarantor4.to_i
return "guarantor4"
else
end
end
def get_user_credit_check(user, contract)
member_position = get_member_position(user, contract)
user_credit_check = member_position + "_credit_check"
return user_credit_check
end
contract = Contract.find(params[:contract_id])
user_credit_check = get_user_credit_check(current_user, contract)
contract.send(user_credit_check) = "Submitted"
contract.save
答案 0 :(得分:2)
您的代码正在尝试分配字符串"已提交"调用方法的结果,即。 user.send(user_credit_check)
。这不是它的工作方式。
如果变量user_credit_check
等于字符串" my_check",并且您想要调用user.my_check
,那么使用send
就可以了。
但是,我假设你想做user.my_check = "Submitted"
之类的事情,这实际上只是user.my_check=("Submitted")
的Ruby语法糖。因此,您要传递给send
的方法实际上是" my_check =",而不是" my_check",而send
的第二个参数是通过,即。 "提交"
总之,您希望执行以下操作:
user.send("#{user_credit_check}=", "Submitted")
顺便说一句,我总是会使用public_send
,而不是send
这样的事情 - 我知道这种情况下的方法是Rails提供的访问器,但以防万一 - 私有方法应该保持私密,这是意外规避这一隐私层的一种方式。