加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为rails中的link_to方法添加onclick选项?

发布时间:2020-12-17 03:20:45 所属栏目:百科 来源:网络整理
导读:我想为link_to方法添加一个onclick选项来加载一个模态对话框…我正在使用rails版本2.3.8并且我在谷歌搜索并且无法做到. Plz有人帮帮我吗? 我的link_to方法如下. %= link_to 'All countries',{:controller='countries',:action='new'},:remote = true % 解决
我想为link_to方法添加一个onclick选项来加载一个模态对话框…我正在使用rails版本2.3.8并且我在谷歌搜索并且无法做到. Plz有人帮帮我吗?

我的link_to方法如下.

<%= link_to 'All countries',{:controller=>'countries',:action=>'new'},:remote => true %>

解决方法

如果您使用的是2.3.8,则没有:remote =>真正.如果您尝试执行ajax操作,则需要使用link_to_remote.

所以它看起来像:

<%= link_to_remote 'All countries',:url => {:controller => 'countries',:action => 'new'}%>
<div id="populate_me"></div>

并且你的新方法必须用类似的东西来处理ajax请求

countries_controller.rb

def new
  <do something>
  render :update do |page|
    page.replace_html 'populate_me',:partial => 'whatever'
  end
end

更新

如果你想要除了ajax动作之外的onclick,你可以将它传递给html选项:

<%= link_to_remote 'All countries',:action => 'new'},:html => {:onclick => 'alert("some javascript executed before ajax")'} %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读