小编Mic*_*hoi的帖子

使用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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

result_refinement.rb

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

  belongs_to :profile
end
Run Code Online (Sandbox Code Playgroud)

ajax jquery link-to-remote ruby-on-rails-3

5
推荐指数
1
解决办法
4433
查看次数

标签 统计

ajax ×1

jquery ×1

link-to-remote ×1

ruby-on-rails-3 ×1