2016-12-01 65 views
-2

我得到SyntaxError - syntax error, unexpected '=', expecting keyword_end当我使用send方法来将字符串转换为我的Rails应用程序中的对象。什么奇怪的是,这是当我使用它的值进行比较的东西,像这样的工作发现:SyntaxError - 语法错误,意外的'=',期望使用send方法时的keyword_end Rails

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 

回答

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-提供访问者,但以防万一 - 私有方法应保持私有,这是意外避开该隐私层的一种方式。

+0

啊谢谢...错误现在对我有意义,这解决了我的问题! –

相关问题