当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - Compute internal redirect URL with parameters

问题描述:

Standard way in ROR to redirect the user is to do:

redirect_to controller: '/some/controller', action: 'some_action', param1: value1, param2: value2, status: :see_other

However in my use case, I want to generate the corresponding URL and render it to the user, which they can click later if they want. Internally, redirect_to uses a method called _compute_redirect_to_location to generate the exact URL string, but as per the naming convention, this does not look like a method that was intended to be used directly by an external code.

Is there a proper way to generate this URL and not have to figure out the route then parse and join the params manually? If not, is it safe to use that method outside of gem?

网友答案:

It seems like you are looking for url_for:

url_for(controller: '/some/controller', action: 'some_action', param1: value1, param2: value2)
#=> "/some/controller/some_action?params1=value1&params2=value2"

url_for also accepts parameters like host or protocol.

Or you can use Rails` routes helper, if you set them up correctly (see: RailsGuides - Rails Routing from the Outside In).

分享给朋友:
您可能感兴趣的文章:
随机阅读: