2011-08-23 52 views
1

我試圖找到一個註冊鏈接點擊的方法。在閱讀了這個主題並嘗試了很多之後,我沒有得到它的功能。我最好的嘗試,這是行不通的,是<%= link_to person.name, person_url, :method => :count_new %>。 person_url是我希望點擊鏈接的人應該訪問的網址。 count_new方法如下所示:計數鏈接點擊

stat = Stat.find_by_country(params[:country]) 
stat = stat.download+=1 
stat.save 

params [:country]在url中。 我知道update_attributes效果更好,但我會在稍後討論。

我研究了在person_url頁面上計數視圖,但我真的想要註冊來自不同鏈接的點擊,而不是在實際頁面用戶上註冊視圖。

我在軌道上3.

任何幫助表示讚賞。

魯特格爾

回答

3
stat = stat.download+=1 
^ ^ ^
model ^ ^
     model ^
      attribute of model 

你試圖整數保存到您的統計模型時,你應該更新它的屬性,下載(我認爲)。

@stat.update_attribute(download, :download += 1) 

編輯

你的link_to看起來關:

<%= link_to person.name, count_person_path, :remote => true %> 

有這個在你的路線,使這項工作:

map.resources do 
    member do 
    get :count 
    end 
end 

還添加軌道3的JavaScript助手遠程=> true call:

http://asciicasts.com/episodes/205-unobtrusive-javascript

+0

馬克,抱歉,我複製的代碼是不正確的。這是stat.download + = 1這與你的建議基本相同。我在這裏遇到的困難不僅僅在於獲得一個計數器,而是要在執行person_url時指望點擊。 – Rutger

+0

很難知道你的問題是什麼,但看看我的編輯,看看它是否有幫助。 – mark

1
@stat.increment_counter('download')