在rails中为link_to方法添加onclick选项?

Ros*_*osh 5 ruby ajax ruby-on-rails modal-dialog

我想为link_to方法添加一个onclick选项来加载一个模态对话框...我正在使用rails版本2.3.8并且我在谷歌搜索并且无法做到.Plz有人帮帮我吗?

我的link_to方法如下.

<%= link_to 'All countries',{:controller=>'countries', :action=>'new'}, :remote => true %>
Run Code Online (Sandbox Code Playgroud)

Chr*_*tto 6

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

所以它看起来像:

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}%>
<div id="populate_me"></div>
Run Code Online (Sandbox Code Playgroud)

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

countries_controller.rb

def new
  <do something>
  render :update do |page|
    page.replace_html 'populate_me', :partial => 'whatever'
  end
end
Run Code Online (Sandbox Code Playgroud)

更新

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

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}, :html => {:onclick => 'alert("some javascript executed before ajax")'} %>
Run Code Online (Sandbox Code Playgroud)