2012-07-10 51 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