根据您访问表单的位置重定向到不同的路径



我在应用程序的两个不同站点使用相同的表单,并且根据我访问表单的位置,我希望在提交后被重定向到不同的路径。

这是我在控制器中的内容:

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然后从中提取路径的麻烦。

最新更新