【发布时间】:2018-10-20 21:53:51
【问题描述】:
Rails 新手,请多多包涵……
我创建了一个日历应用,日历的所有者可以将其他用户添加到日历中。我刚刚意识到他们可以多次添加同一个用户,这显然是我不想要的。我尝试了很多不同的方法,包括检查模型内部的方法,但没有奏效……我正在使用包含来验证……
我的控制器中的当前方法是:
def update
if @calendar.calendar_admin?(current_user)
@new_user = User.find(params[:calendar][:user_ids])
if @calendar.users.includes(@new_user)
redirect_to user_calendar_path(@calendar), notice: 'This user has already been added to this calendar.'
else
@calendar.users << @new_user
if @calendar.save
redirect_to user_calendar_path(@calendar), notice: 'Your calendar has been updated.'
else
redirect_to user_calendar_path(@calendar)
end
end
end
end
结束
无论如何,即使用户没有,它也会卡在“此用户已添加到此日历中”。我正在铲除新用户,但我什至不确定这是否是添加新用户的正确方式?
输入?
【问题讨论】:
-
应该是
include?而不是includes?
标签: ruby-on-rails duplicates include exists verification