0

I have 3 link with images. When I click on link I want add my count +1.

<a href="/home" id="push"><img src="/images/image1.gif" /></a>
<a href="/faq"> <img src="/images/image2.gif" /></a>
<a href="/news"><img src="/images/image3.gif" /></a>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script type="text/javascript">
  $("#push").click(function(){
      $.ajax({
        url: "room/room_count", type: "POST"});
  });
</script>

in my controller

 def room_count
   count_method = Room.find 1
   count_method.update_attribute(:count, count_method.count.to_i + 1)
 end

If I click on link count don't update, but if I replase href="/home" on href="#" all works fine.

What am I doing wrong?

Thanks in advance

4

1 回答 1

0

将 onclick 放在 href 中会冒犯那些坚信内容与行为/动作分离的人。争论是你的 html 内容应该只关注内容,而不是表现或行为。

这些天的典型路径是使用 javascript 库(例如 jquery)并使用该库创建事件处理程序。它看起来像:

$("#push").click(function(){
      $.ajax({
        url: "room/room_count", type: "POST"});
      return false;
  });
于 2013-09-06T14:33:48.110 回答