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

Mic*_*hoi 5 ajax jquery link-to-remote ruby-on-rails-3

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

Dea*_*age 6

您需要先设置路线ProfileController#refine_result.就像是

match '/profile/refine_results' => 'profile#refine_results', :as => 'refine_results'
Run Code Online (Sandbox Code Playgroud)

然后你可以使用

.status-bar
  = link_to "", refine_results_url(profile_id: 1, message_id: 100, new_rating: "awful"), remote: true
Run Code Online (Sandbox Code Playgroud)