Ruby on rails 在rails中添加onclick选项以将_链接到方法?

Ruby on rails 在rails中添加onclick选项以将_链接到方法?,ruby-on-rails,ruby,ajax,modal-dialog,Ruby On Rails,Ruby,Ajax,Modal Dialog,我想添加一个onclick选项,将_链接到加载模式对话框的方法……我使用的是rails版本2.3.8,我在google上搜索了一下,但没有找到。有人能帮我吗 我的方法链接如下 <%= link_to 'All countries',{:controller=>'countries', :action=>'new'}, :remote => true %> 'countries',:action=>'new'},:remote=>true%> 如果您使用的是2.3.

我想添加一个onclick选项,将_链接到加载模式对话框的方法……我使用的是rails版本2.3.8,我在google上搜索了一下,但没有找到。有人能帮我吗

我的方法链接如下

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

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

所以它看起来像:

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}%>
<div id="populate_me"></div>
{:controller=>'countries',:action=>'new'}%>
您的新方法必须使用以下方式处理ajax请求

国家/地区控制器.rb

def new
  <do something>
  render :update do |page|
    page.replace_html 'populate_me', :partial => 'whatever'
  end
end
def新建
渲染:更新do |页面|
page.replace_html'populate_me',:partial=>which'
结束
结束
更新

如果除了ajax操作之外还需要onclick,可以将其传递到html选项中:

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}, :html => {:onclick => 'alert("some javascript executed before ajax")'} %>
{:controller=>'countries',:action=>'new'},:html=>{:onclick=>'alert(“在ajax之前执行的某些javascript”)}%>

您可以将此添加到链接:

, :class => "pop light", :id => "modal_link"
然后,您的JS显示了类似这样的内容:

<script type="text/javascript">
    $(document).ready(function() {
      $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"></a>');
        $('a.close').hide();
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;
        $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });
        $('body').append('<div id="fade"></div>');
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
        return false;
      });
      $('a.close').live('click', function() {
          $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove(); 
          });
          return false;
      });         
      $('#modal_link').click();
    });
  </script>

$(文档).ready(函数(){
$('a.poplight[href^=#]')。单击(函数(){
var popID=$(this.attr('rel');//获取弹出窗口名称
var popURL=$(this.attr('href');//获取Popup href以定义大小
var query=popURL.split(“?”);
var dim=query[1]。拆分('&');
var popWidth=dim[0]。拆分('=')[1];//获取第一个查询字符串值
$('#'+popID).fadeIn().css({'width':Number(popWidth)}).prepend('');
$('a.close').hide();
var popMargTop=($('#'+popID).height()+80)/2;
var popMargLeft=($('#'+popID).width()+80)/2;
$('#'+popID).css({
“页边距顶部”:-popMargTop,
“左边距”:-popMargLeft
});
$('body')。追加('');
$('#fade').css({'filter':'alpha(不透明度=80)}).fadeIn();
返回false;
});
$('a.close').live('click',function()){
$('#淡入,.popup#u块')。淡出(函数(){
$(“#淡入,a.close”).remove();
});
返回false;
});         
$(“#模态链接”)。单击();
});

谢谢克里斯。。我将部分内容加载到该分区中。。但我想加载模态对话框。我应该把代码放在哪里?我是新手,很抱歉问了一些愚蠢的问题。