0

I took a look at various posts like this How to use the link_to helper to open a popup? but I wasn't able to make it work.

Here is my code:

in the application.js I included this code:

$('a[data-popup]').live('click', function(e) {
  window.open( $(this).attr('href'), "Popup", "height=600, width=600" );
  e.preventDefault();
});

in the assessment_controller.rb I included this method:

  def open_second_app
    respond_to do |format|
      format.js
    end
  end

in the assessments index page index.html.erb I included the link:

<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>

I also have a open_second_app.js.erb file containing this single line:

window.open (<%= open_second_app_path %>, "Second Appllication Window", "width=600,height=600");

When I click the link, I am getting this error:

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>

The main application is running as localhost:3000. The second application is running as localhost:3001.

What I am doing wrong here?

Community
  • 1
  • 1
L.D
  • 1,279
  • 3
  • 15
  • 31

1 Answers1

1

To open in a new window you need to set the target to blank. See this post: How to use "_blank" or "_new" in Rails

Updating to account for the question you made in a comment. To use a different url in production than in development:

<% if Rails.env.production? %>
  <% second_app_url = 'http://whatever' %>
<% else %>
  <% second_app_url = 'http://localhost:whatever' %>
<% end %>

Obviously better to handle this in a method than in your views but just showing you the condition for example purposes.

Community
  • 1
  • 1
toddmetheny
  • 4,405
  • 1
  • 22
  • 39
  • I changed the line in the index page (as indicated) as this: ` <%= link_to( 'Open Second Application', 'http://0.0.0.0:3001', 'data-popup' => true, target: '_blank' ) %>` and is working. I am starting the main app with the command `rails s` and the second app wth command `rails s -p 3001 -b 0.0.0.0`. Is there anything I need to change when deploying with `heroku` ? – L.D Mar 19 '16 at 18:59
  • Just the production url. – toddmetheny Mar 19 '16 at 19:00
  • But if am replacing 0.0.0.0 with my-second-app.herokuapp.com the development for first app will use the same heroku deployed second app instead of the development instance. – L.D Mar 19 '16 at 19:26
  • just add a condition for using the different urls in development. I'll update my answer to show you how. – toddmetheny Mar 19 '16 at 19:29