微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

所以我有一个包含多个消息的页面,每个消息都有一个链接,用于更改(精炼)该消息的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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐