开发者

Popup/New Window + Redirect_to + Rails

开发者 https://www.devze.com 2022-12-08 22:40 出处:网络
It is possible to redirect_to some 开发者_JS百科url from the controller and open the url in the new window at the same time ?Not really from the controller action.

It is possible to redirect_to some 开发者_JS百科url from the controller and open the url in the new window at the same time ?


Not really from the controller action.

A redirect is an HTTP response with a 301/302 status code and an empty body in Rails. As the body is empty, you don't have any way to include extra processing for the browser (such as javascript etc to open a new window).

Assuming you have the the controller action triggered via a hyperlink on another page, your best bet would be to have that open in a new window. Then the redirect target would be displayed.

Either that, or render the same page again as a regular response (no redirect), and insert some javascript when the page loads to open a new window


@madlep "your best bet would be to have that open in a new window"

Thanks for that one. Works perfectly!

view:

<%= link_to "Buy this product", buy_path, target: "_blank" %>

controller:

def buy
  ...
  redirect_to link
end


Searched a little and found a solution... it's not on the controller (as I originally wanted too but it's quite easy...) in your view:

<%= link_to '', {:controller => 'assignments', :action => 'overall_report_gen', :evaluator_relations => @evaluator_relations}, :target => '_blank', :id => 'reporte' %>

<script language="JavaScript">
  document.getElementById('reporte').click();
</SCRIPT>

so, with '' as the display string in the link_to, you get an invisible link,

with the :target => '_blank' the link, when clicked will open in a new window

and the :id => 'reporte' is needed in order to click the link with javascript.

so just place the same id in the link_to and in the string in the javascript, and that's it

remember to place the javascript snippet AFTER the link_to so as to wait the element is loaded

0

精彩评论

暂无评论...
验证码 换一张
取 消