加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – 使用link_to remote:true将参数传递给rails

发布时间:2020-12-16 02:52:36 所属栏目:百科 来源:网络整理
导读:所以我有一个包含多个消息的页面,每个消息都有一个链接,用于更改(精炼)该消息的RATING.当用户单击此链接时,我想要一个 AJAX调用,它会更新该消息的数据库中的相应列值.单击此链接时,不会发生任何明显的情况.应该没有页面刷新或重新加载. 我一直在尝试使用link
所以我有一个包含多个消息的页面,每个消息都有一个链接,用于更改(精炼)该消息的RATING.当用户单击此链接时,我想要一个 AJAX调用,它会更新该消息的数据库中的相应列值.单击此链接时,不会发生任何明显的情况.应该没有页面刷新或重新加载.

我一直在尝试使用link_to remote:true,但我似乎无法让它工作.在线文档在这个问题上相当不清楚,并且随着Rails 2和3之间的变化,一些事情如:不再支持.

到目前为止,我已经复制了我所拥有的内容,但我知道它甚至不会接近解决方案.
在我需要传递到数据库的参数方面,我需要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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读