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

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

提问人:seitam 提问时间:4/6/2020 更新时间:4/6/2020 访问量:240

问:

这是我遇到的错误:

语法错误,意外的“=”,期望结束@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

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

ruby-on-rails Ruby 语法错误

评论

0赞 Stefan 4/6/2020
要调用 setter,请使用 – 是方法名称的一部分。@subscripting.send("#{name}=", false)=
0赞 max 4/6/2020
你为什么还要在这里使用?与用户输入一起使用非常危险,尤其是在您没有将输入列入白名单的情况下。sendsend

答:

0赞 lacostenycoder 4/6/2020 #1

使用时,将参数作为辅助参数传递给方法。请参阅文档 https://ruby-doc.org/core-2.7.1/Object.html#method-i-sendObject.send

因此,请尝试:

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

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