在这个问题上,我一直在用头撞墙--我有一些代码用来测试用户是否试图访问他们关联的帐户,如果没有,就将他们重定向到自己的帐户。基本上,它是为了阻止人们通过URL路由帐户访问他们自己的帐户。
这段代码在我构建的其他站点上运行正常,但由于某种原因,它在新项目中不起作用。
有问题的代码:
def set_company
if params[:id].nil?
@company = Company.find(current_contact.company_id)
else
@company = Company.find(params[:id])
if @company.id != current_contact.company_id
redirect_to(company_path(current_contact.company_id), alert: 'You can\'t access that account This is not your account!!! This one is.' )
end
end
end从理论上讲,当if语句返回true时,它应该重定向到当前用户设置了params ID的company_path。在第二次运行这段代码时,它应该根据params id找到公司,然后在检查公司id是否与当前用户的公司ID不匹配时返回false,但是我得到了一个"this page has a redirect loop“错误。
发布于 2014-08-13 19:29:51
而不是使用find方法访问db -我认为您会更好地针对纯值进行验证,然后才执行find方法:
def set_company
if params[:id].nil?
@company = Company.find(current_contact.company_id)
else
redirect_to(company_path(current_contact.company_id), alert: 'You can\'t access that account This is not your account!!! This one is.' ) if params[:id] != current_contact.company_id
@company = Company.find params[:id]
end
end发布于 2014-08-13 19:55:55
试用this..using present
def set_company
if params[:id].present?
@company = Company.find(current_contact.company_id)
else
@company = Company.find(params[:id])
if @company.id != current_contact.company_id
redirect_to(company_path(current_contact.company_id), alert: 'You can\'t access that account This is not your account!!! This one is.' )
end
end
endhttps://stackoverflow.com/questions/25284331
复制相似问题