我在应用程序的两个不同站点使用相同的表单,并且根据我访问表单的位置,我希望在提交后被重定向到不同的路径。
这是我在控制器中的内容:
def create
@item = Offer.new(offer_params)
authorize @item
if @item.save
condition_I_don't_know_how_to_write ? redirect_to(x_path) : redirect_to(y_path)
else
render :new
end
end
您可以找到请求源自request.referrer
的URL。它将以https://example.com/products/1
格式提供整个URL。然后,您可以很容易地从该URL中提取所需的信息,例如URI(request.referrer).path
的路径,在本例中将返回/products/1
。
另一个选择是使用嵌套路由。通过这种方式,您可以将request.path
设置为类似/products/1/offers/new
的内容,这样可以减少歧义并避免获取request.referrer
然后从中提取路径的麻烦。