我有一个链接,它使用 put 方法在投票时发送 ajax 请求。它在启用 javascript 的情况下工作正常。所以当我禁用 javascript 时它发送一个 get 请求。我什至尝试在 link_to 中使用方法 post 没有运气。任何帮助将不胜感激..
路线
resources :coupon_votes ,:only => [:voted_down,:voted_up] do
member do
put 'voted_up'
put 'voted_down'
end
end
看法
link_to(image_tag("coupon/works_yes_icon.png",:alt => "Worked",:size =>"16x15"),voted_up_coupon_vote_path(object.id),:html => {:remote => true, :method=>:put},:id => "voted_up_voted_up_#{object.id}",:class => "working")
link_to(image_tag("coupon/works_no_icon.png",:alt => "Did not work",:size => "16x15"),voted_down_coupon_vote_path(object.id),:remote => true, :method=>:put , :id => "voted_up_voted_down_#{object.id}",:class => "not_working"))
控制器
def voted_up
comman_vote do
@coupon.vote_up(current_user)
end
end
def voted_down
comman_vote do
@coupon.vote_down(current_user)
end
end
def comman_vote
if current_user && current_user.active?
yield
end
respond_to do |format|
format.js {render "coupon_voted"}
format.html {render :text => "Thanks for the vote! Please enable javascript to use all the features of the website."}
end
end
def find_coupon
return if !current_user
@coupon=Coupon.find(params[:id])
end
更新
谢谢大家的努力。我做了另一种方式,而不是在我在 javascript(application.js) 中做的 link_to 中使用 remote true 。
意见
link_to(image_tag("coupon/works_yes_icon.png",:alt => "Worked",:size => "16x15"), "#", :data => {"source" => object.id },:id => "voted_up_voted_up_#{object.id}",:class => "working")
应用程序.js
$(".working").click(function (){
var coupon_id = $(this).data('source');
var request = $.ajax({url: '/coupon_votes/' + coupon_id + '/voted_down',
type: 'POST',
dataType: 'script'
})
return false;
});