我看了像这样的各种帖子How to use the link_to helper to open a popup?
但我无法使其正常运行。
这是我的代码:
在application.js
中,我包含了以下代码:
$('a[data-popup]').live('click', function(e) {
window.open( $(this).attr('href'), "Popup", "height=600, width=600" );
e.preventDefault();
});
在
assessment_controller.rb
中,我包含了此方法: def open_second_app
respond_to do |format|
format.js
end
end
在评估索引页面
index.html.erb
中,我包含了以下链接:<div>
<%= button_to 'New', new_assessment_path, method: :get %>
<%= button_to 'Home', root_path, method: :get %>
<%= link_to( 'Open Second Application', open_second_app_path, 'data-popup' => true ) %>
</div>
我也有一个
open_second_app.js.erb
文件,其中包含以下这一行:window.open (<%= open_second_app_path %>, "Second Appllication Window", "width=600,height=600");
单击链接时,出现此错误:
Showing /Users/liviu-mac/ror/levi-test-01/app/views/assessments/index.html.erb where line #34 raised:
undefined local variable or method `open_second_app_path' for #<#<Class:0x007fe3c6fa4040>:0x007fe3c40ae4c0>
主应用程序以
localhost:3000
运行。第二个应用程序以
localhost:3001
运行。我在这里做错了什么?
最佳答案
要在新窗口中打开,需要将目标设置为空白。看到这篇文章:How to use "_blank" or "_new" in Rails
更新以解决您在评论中提出的问题。要在生产环境和开发环境中使用不同的url,请执行以下操作:
<% if Rails.env.production? %>
<% second_app_url = 'http://whatever' %>
<% else %>
<% second_app_url = 'http://localhost:whatever' %>
<% end %>
在方法中处理此问题显然比在您的视图中处理好,但仅出于示例目的向您显示条件。