簡體   English   中英

為什么我會收到錯誤消息? (語法錯誤,意外的'=',期待結束)

[英]Why am I getting an error? (syntax error, unexpected '=', expecting end)

這是我得到的錯誤:

語法錯誤,意外 '=',期待結束 @subscripting.send(name) = false ^

這是我的代碼:

看法

<% if @subscripting.send(service.name) == true %>
    <p>done</p>
    <p>password<%= service.password %></p>
    <%= link_to "cancel", cancel_path(service_name: service.name), :method => :post %>
<% else %>

controller

def cancel
    name = params[:service_name]
    @subscripting = Subscripting.find_by(user_id: @current_user.id)
    @subscripting.send(name) = false
end

非常感謝您的回答和幫助。

當使用Object.send時,您將 arguments 作為輔助 arguments 傳遞給該方法。 請參閱文檔https://ruby-doc.org/core-2.7.1/Object.html#method-i-send

所以改為嘗試:

def cancel
  name = params[:service_name]
  @subscripting = Subscripting.find_by(user_id: @current_user.id)
  @subscripting.send(name, false)
end    

但是如果參數來自某些用戶輸入,這可能很危險,您應該考慮使用強參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM