Reputation: 30385
I just want to use link_to
to open a popup. I tried something but it doesn't work:
<%= link_to 'Create a new company',
new_company_path,
:popup => ['create_company', 'height=600, width=600'] %> <br/>
Any idea?
Thanks!
Upvotes: 11
Views: 19917
Reputation: 18080
Add this to your application.js.
$('a[data-popup]').on('click', function(e) { window.open($(this).attr('href')); e.preventDefault(); });
In the view, use something like:
= link_to( 'Create a new company', new_company_path, 'data-popup' => true )
Upvotes: 19
Reputation: 10705
If your goal is just to open the link in a new window and you don't care about managing the dimensions/toolbar/etc, you can also use good old HTML:
<%= link_to 'Create a new company', new_company_path, :target => '_blank' %>
Upvotes: 2
Reputation: 8884
<%= link_to 'Create a new company',
new_company_path,
:onclick=>"window.open(this.href,'create_company', 'height=600, width=600');return false;"
%>
Upvotes: 11
Reputation: 8220
This is the quick and dirty solution
<%= link_to 'Create a new company',
'#', :onclick => "javascript:window.open(new_company_path,'popup','width=600,height=600');" %>
Upvotes: 1
Reputation: 1690
My first stab at this problem would probably look something like this. It assumes you're using rails 3, jQuery and jquery-rails. If you're not, this approach definitely won't work. This exact code isn't tested, so your mileage may vary. I'm just trying to give you an idea on how you might want to think about the problem. If you'd like me to elaborate on how this works, or have questions, let me know and I'll do my best to explain.
Turn your link_to into an ajax post:
<%= link_to "Create a new company", new_company_path, :remote => true, :method => :post %>
In your controller, respond with a javascript template:
def create
@company = Company.new(params[:company])
respond_to do |format|
if @company.save
format.js
else
format.js { render 'error' }
end
end
end
In views/companies/create.js.erb, execute the JS to open the new window.
window.open (<%= company_url(@company) %>, "mywindow","width=600,height=600");
And that should more or less do it, I think. I've had a few beers, so proceed with caution.
Upvotes: 6