提问人:seitam 提问时间:4/6/2020 更新时间:4/6/2020 访问量:240
为什么我会收到错误?(语法错误,意外的“=”,预期结束)
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 %>
控制器
def cancel
name = params[:service_name]
@subscripting = Subscripting.find_by(user_id: @current_user.id)
@subscripting.send(name) = false
end
非常感谢您的回答和帮助。
答:
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
但是,如果参数来自某些用户输入,这可能很危险,您应该考虑使用强参数
评论
@subscripting.send("#{name}=", false)
=
send
send