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

时间:2021-12-19 11:22:54

I want to add an onclick option to link_to method for loading an modal dialog box...i am using rails version 2.3.8 and i searched on google and could not do it. Plz anybody help me?

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

My link_to method as follows.

我的link_to方法如下。

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

2 个解决方案

#1


6  

If you are using 2.3.8, you don't have :remote => true. You need to use link_to_remote if you are try to do an ajax action.

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

So it would look something like:

所以它看起来像:

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

and your new method would have to handle the ajax request with something like

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

countries_controller.rb

countries_controller.rb

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

UPDATED

更新

If you want the onclick in addition to the ajax action, you can just pass it into the html options:

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

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

#2


0  

You can add this to the link:

您可以将其添加到链接:

, :class => "pop light", :id => "modal_link"

Then, your JS shows something ilke this:

然后,你的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>

#1


6  

If you are using 2.3.8, you don't have :remote => true. You need to use link_to_remote if you are try to do an ajax action.

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

So it would look something like:

所以它看起来像:

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

and your new method would have to handle the ajax request with something like

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

countries_controller.rb

countries_controller.rb

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

UPDATED

更新

If you want the onclick in addition to the ajax action, you can just pass it into the html options:

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

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

#2


0  

You can add this to the link:

您可以将其添加到链接:

, :class => "pop light", :id => "modal_link"

Then, your JS shows something ilke this:

然后,你的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>