如何使用 link_to 助手打开弹出窗口? [英] How to use the link_to helper to open a popup?

查看:19
本文介绍了如何使用 link_to 助手打开弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用 link_to 打开一个弹出窗口.我尝试了一些方法,但它不起作用:

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

有什么想法吗?

谢谢!

推荐答案

我第一次尝试这个问题可能看起来像这样.它假设您使用的是 rails 3、jQuery 和 jquery-rails.如果你不是,这种方法肯定行不通.此确切代码未经测试,因此您的里程可能会有所不同.我只是想告诉你你可能想如何思考这个问题.如果您希望我详细说明这是如何工作的,或者有任何疑问,请告诉我,我会尽力解释.

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.

将您的 link_to 转换为 ajax 帖子:

Turn your link_to into an ajax post:

<%= link_to "Create a new company", new_company_path, :remote => true, :method => :post %>

在您的控制器中,使用 javascript 模板进行响应:

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

在views/companies/create.js.erb中,执行JS打开新窗口.

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.

这篇关于如何使用 link_to 助手打开弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆