gpt4 book ai didi

ruby-on-rails - Controller 操作引发错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:10 24 4
gpt4 key购买 nike

我有一个 Controller 方法名称 process_message,它的目的是根据收到的消息将人们注册到特定的组。现在一切正常,除了它会一遍又一遍地将一个人注册到同一个组。我正在尝试添加一些快速代码来实现,以便他们只能注册到特定组一次。我以为我想通了,但是当我尝试执行我写的内容时出现错误。为了清楚起见,我将显示一些屏幕截图和一些代码。

这是我的 Controller 方法

def process_message
if message_params[:action] == 'subscribe'
# result = SubscribeToGroup.call(message_params)
# results.success? ? result.message : result.error
message_params[:id] = "visitor" if message_params[:id] == "tulip" # hack for now
@group = Group.find_by(name: message_params[:id])
if @group && @subscriber.groups << @group unless groups.include? (@group) ^
@group.response
else
'You cannot subscribe to unknown group'
end
elsif message_params[:action] == 'stop'
# result = UnsubscribeToGroup.call(message_params)
# results.success? ? result.message : result.error
group_name = message_params[:id]
@group = @subscriber.groups.find_by(name: group_name)
@subscriber.groups.destroy(@group) if @group
"You are now unsubscribed from #{group_name.upcase} notifications. Text '#{group_name.upcase}' to start receieving updates again."
else
"Sorry, we don't recognize that command."
end
rescue Exception => e
"Something went wrong. Try again. #{e.message}"
end

您可以在第 7 行看到我有一些代码,我认为这些代码可以防止消息两次注册一个人,但它不起作用,这是我运行 Rspec 时的错误消息。

enter image description here

我希望我的问题很清楚,如果您需要更多代码,请告诉我。

最佳答案

我会避免将 ifunless 放在同一行

这是你想要做的吗?

# You may have meant to provide `@subscriber.groups` instead of `groups` here. I'm not sure from information provided.
unless groups.include?(@group)
@subscriber.groups << @group
end

if @group && @subscriber.groups.present?
# ...

关于ruby-on-rails - Controller 操作引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com