2012-07-10 53 views
5

因此,我有一個包含多條消息的頁面,每條消息都有一條鏈接,用於更改(優化)該消息的RATING。當用戶點擊這個鏈接時,我想要一個AJAX調用來更新該消息的數據庫中相應的列值。點擊此鏈接時,不會有任何明顯的事情發生。應該沒有頁面刷新或重新加載。使用link_to remote:true將參數傳遞給rails

我一直在試圖做到這一點使用link_to遠程:真,但我似乎無法得到它的工作。在線文檔在這個問題上還不太清楚,並且隨着Rails 2和3之間的變化,有些內容如:with不再受支持。

我已經複製了迄今爲止我所擁有的內容,但我知道它甚至還沒有接近解決方案。 根據我需要傳入數據庫的參數,我需要profile_id,message_id和new_rating。

在此先感謝!

show.html.haml

.status-bar 
    = link_to "", { action: :refine_result }, remote: true 

profile_controller.rb

... 

def refine_result 
    @refinement = ResultRefinement.new 
    @refinement.profile_id = params[:profile_id] 
    @refinement.message_id = params[:message_id] 

    @refinement.save 

    respond_to do |format| 
    format.html { render nothing: true } 
    format.js { render nothing: true } 
    end 
end 

result_refinement.rb

class ResultRefinement < ActiveRecord::Base 
    attr_accessible :profile_id, :message_id, :new_rating, :deleted 

    belongs_to :profile 
end 

回答

6

您需要設置一個路由,ProfileController#refine_result第一。像

match '/profile/refine_results' => 'profile#refine_results', :as => 'refine_results' 

東西然後你可以使用

.status-bar 
    = link_to "", refine_results_url(profile_id: 1, message_id: 100, new_rating: "awful"), remote: true 
+0

真棒。我得到它開始傳遞參數並將它們保存到數據庫中。 我仍然遇到的唯一問題是停止重定向或更改頁面。 – 2012-07-11 16:04:36

+0

看來,另一個JS文件覆蓋了AJAX。這就是爲什麼我繼續遵循鏈接而不是讓Ajax接管。 – 2012-07-11 19:36:29