English 中文(简体)
铁路和JQuery——能够脱钩的违约行动
原标题:Rails and JQuery - can t disable a links default action

我有一个“核对”的链接,与同名控制者的行动相径庭。 其路线是作为POST的要求。 Ive set up the Code but what whening is the jquery$.post fires as Hope, but the html request also when is mean my action is being used two. 我可以说明原因。 谁会发现错误吗?

我的申请。 j)

jQuery.ajaxSetup({
   beforeSend : function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

$(document).ready(function() {
 $( ul li.sentence_summary a.correct ).click(function(){
      $(this).css("border", "1px black solid");
      $.post($(this).attr("href"), null, null, "script");
      return false;
    });

});

纽约总部

 def check
    @sentence_author = User.find(params[:user_id])
    @sentence = @sentence_author.sentences.find(params[:id])
    @sentence.checked_at = Time.now
    @sentence.checker_id = current_user.id
    respond_to do |format|
      if @sentence.save!
        flash[:notice] = "Thank you for checking #{@sentence_author.to_s}s sentence."
        format.js {  }
        format.html { redirect_to user_foreign_sentences_path(current_user, :display => :pending) }
        format.xml  { head :ok }
      end
    end
  end

和诉讼案

alert("testing123");

<>>>>>

我认为,可以部分地作出判决。 部分转而采用下文所示的观点帮助方法,使链接得以链接。

%li{:id => ["sentence", "#{sentence.id}"], :class => "sentence_summary bottom_border_light"}
  %div.left
    = thumbnail_link_to_user_profile(sentence.user, User::GRAVATAR_M)
  %div.left.main_info_container
    %div
      = link_to_user_profile(sentence.user)
      = t( sentences.table.wrote )
    %div.main_focus

      = sentence.text
    %div.bottom_bar
      %div.left
        = created_at_linked_to_sentence_show_path(sentence)
      %div.right.rollover_options
        = render_check_options(sentence)
        = render_edit_options(sentence)
        = render_flag(sentence)
      %div.clear
  %div.clear

以及这一助手使有关联系得以实现

  def render_check_options( sentence)
    if sentence.user != current_user and sentence.language == current_user.native_language and sentence.pending?
      html = link_to("correct", check_user_sentence_path(sentence.user, sentence), :method => "post", :class => "action_options underline_on_hover always_blue correct")
      html += link_to("incorrect", new_sentence_correction_path(sentence), :class => "action_options underline_on_hover always_blue incorrect")
      return html
    end
  end

UPDATE 2* as per tonys advice heres what happens in my firebug console

console.debug($( ul li.sentence_summary a.correct ));

预计正确链接的产出(点击这些链接,然后到正确的html)

[a.action_options /users/9...24/check, a.action_options  /users/2...26/check, a.action_options /users/2...27/check, a.action_options  /users/9...28/check, a.action_options /users/1.../8/check, a.action_options  /users/2...10/check, a.action_options /users/1...11/check, a.action_options  /users/9...12/check]

即便在接任以下两任指挥官之后,这些联系也并非残疾人。

   $( ul li.sentence_summary a.correct ).unbind( click );


    $( ul li.sentence_summary a.correct ).click(function(){
          return false;
        });  

我的html看一看这ul头一li(指出,除非滚动,否则有关联系是隐蔽的,因此,这里的显示有:

        <div class="left">
          <a href="/users/9"><img alt="D03e17968fe80cd2d3816e86a7e072f9" class="avatar" src="http://gravatar.com/avatar/d03e17968fe80cd2d3816e86a7e072f9.png?d=identicon&amp;r=PG&amp;s=45"></a>
        </div>
        <div class="left main_info_container">
          <div>
            <a href="/users/9" class="user_name_link underline_on_hover always_blue">maxwell.wiegand</a>
            wrote
          </div>
          <div class="main_focus">
            Aspernatur eum tenetur dolorem impedit dolor modi illum.
          </div>
          <div class="bottom_bar">
            <div class="left">
              <a href="/users/9/sentences/24" class="meta-data underline_on_hover">2 days ago</a>
            </div>
            <div style="display: none;" class="right rollover_options">
              <a href="/users/9/sentences/24/check" class="action_options underline_on_hover always_blue correct" onclick="var f = document.createElement( form ); f.style.display =  none ; this.parentNode.appendChild(f); f.method =  POST ; f.action = this.href;var m = document.createElement( input ); m.setAttribute( type ,  hidden ); m.setAttribute( name ,  _method ); m.setAttribute( value ,  post ); f.appendChild(m);var s = document.createElement( input ); s.setAttribute( type ,  hidden ); s.setAttribute( name ,  authenticity_token ); s.setAttribute( value ,  ZD3boP6x3HjxzvLBQmTsJT2xCWGQoq8j7M0ZSD6UGbE= ); f.appendChild(s);f.submit();return false;">correct</a><a href="/sentences/24/corrections/new" class="action_options underline_on_hover always_blue incorrect">incorrect</a>

              <a href="/sentences/24/flags/new" class="action_options underline_on_hover always_blue">flag</a>
            </div>
            <div class="clear"></div>
          </div>
        </div>
        <div class="clear"></div>
最佳回答

拆除

$(this).css("border", "1px black solid");
      $.post($(this).attr("href"), null, null, "script");

只是假回,告诉我发生的事情

<>>>>

如果你把一把 link光带回来,那就不应该。 我将检查两件事:

1) Make sure the selector is correct: what is the output when you do this?

console.debug($( ul li.sentence_summary a.correct ));

2) Make sure no other click handlers are interfering: After you have checked #1, open up your JS console and do:

$( ul li.sentence_summary a.correct ).unbind( click );

这将排除所有点击事件,包括你想要发生的事件。 然后,通过执行你在奥索尔的惯常守则(只有点击手里的假回来),将点击事件进行了合并。 这将确保只有一人听从你的联系点击事件,一名手势者会回错,不执行这一联系。

3) 张贴有关产生的超文本,以便我们能够采取眼光

问题回答
html = link_to("correct", check_user_sentence_path(sentence.user, sentence), :method => "post", :class => "action_options underline_on_hover always_blue correct")

<代码>:method =>“post”可能是你在此提出的问题的根源。 这说明 建造提交表格的铁路,意思是:return false; 是你认为做的。

我将努力这样做:

   $( ul li.sentence_summary a.correct ).click(function(e){
      e.preventDefault();
      $(this).css("border", "1px black solid");
      $.post($(this).attr("href"), null, null, "script");
    });




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签