为什么会出现错误? (语法错误,意外的“ =”,预期结束)

时间:2020-04-06 01:14:26

标签: ruby-on-rails ruby syntax-error

这是我遇到的错误:

语法错误,意外的'=',期望结尾@ 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 %>

控制器

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

非常感谢您的回答和帮助。

1 个答案:

答案 0 :(得分:0)

使用Object.send时,您将参数作为辅助参数传递给方法。请参阅文档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    

但是,如果参数来自某些用户输入,则可能很危险,您应该考虑使用Strong Parameters