我在我的网站上使用了 Ruby on Rails。在一个网页中,它将根据投票 ID 加载投票信息,该 ID 设置在 URL 中,如“http://mywebsite/polls/1”。投票信息包括投票所有者姓名、投票标题、投票中的项目图片以及在投票中投票的人的姓名。
我发现有时它加载了错误的信息。也就是说,它加载了错误的投票所有者姓名、投票标题和从另一个投票中投票的人,而项目图片是正确的。我检查了后端,发现 Rails 控制器没有任何问题。所有变量都得到了正确的值。但是chrome浏览器告诉我视图是错误的。
如果我清除了所有缓存并重新加载页面,那么它将正常工作。任何人都知道为什么会发生这种情况,我该怎么办?谢谢
相关动作代码:</p>
def show
@poll=Poll.where("is_deleted = false AND id = #{params[:id]}")[0]
@items=@poll.items.where("is_deleted = false")
@voted_user_ids = @poll.audiences.where('has_voted != 0').map(&:user_id).uniq
@voted_users = User.where('id IN (?)',@voted_user_ids)
@voted_user_names = @voted_users.map(&:user_name)
if current_user.nil?
@poll_vote_url = "/voted_choice"
else
@current_user_name = current_user.user_name
@poll_vote_url = "/audiences/"+@poll.id.to_s
@if_current_user_voted = @voted_users.include?(current_user)
@is_poll_owner = (current_user == @poll.user)
check_item_id_in_cookies
end
end
def check_item_id_in_cookies
if !cookies.signed[:item_id].nil?
item = Item.find(cookies.signed[:item_id].to_i)
#create audience if the voter is not the poll owner
if current_user == item.poll.user
flash.now[:alert] = "You can't vote on your own poll."
cookies.signed[:item_id] = nil
else
create_audience cookies.signed[:item_id]
end
end
end
def create_audience item_id
@item_id = item_id.to_i
@item = Item.find(@item_id)
@poll = @item.poll
@voted_user_ids = @poll.audiences.where('has_voted != 0').map(&:user_id).uniq
if @voted_user_ids.include?(current_user.id)
flash.now[:alert]="You already voted."
else
audience = @item.poll.audiences.find{|audience| audience.user_id == current_user.id} || Audience.create(:poll_id => @poll.id,:user_id => current_user.id)
#update audience
audience.is_following = true
audience.has_voted = @item.id
audience.save
cookies.signed[:item_id]=nil
flash[:alert]="Thank you for your vote"
redirect_to "/polls/#{@poll.id}"
end
end