2016-12-01 67 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

啊謝謝...錯誤現在對我有意義,這解決了我的問題! –

相關問題