gpt4 book ai didi

ajax - 使用 link_to 远程 : true to pass parameters to rails

转载 作者:行者123 更新时间:2023-12-03 23:03:54 25 4
gpt4 key购买 nike

所以我有一个包含多条消息的页面,每条消息都有一个可以更改(细化)该消息的评级的链接。当用户单击此链接时,我需要一个 AJAX 调用来更新数据库中该消息的相应列值。单击此链接时,不会发生任何可见的事情。不应有页面刷新或重新加载。

我一直在尝试使用 link_to remote: true 来做到这一点,但我似乎无法让它工作。在线文档对此问题相当不清楚,并且随着 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

最佳答案

您需要先为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

关于ajax - 使用 link_to 远程 : true to pass parameters to rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com