简体   繁体   中英

ajax request on link_to with method :put in rails with javascript disabled

I have a link which send a ajax request on vote with method put .Its working fine with javascript enabled .So when i disable a javascript it send a get request .I even tried using method post in link_to no luck.Any help would be appreciated ..

routes

resources :coupon_votes ,:only => [:voted_down,:voted_up] do 
  member do 
    put 'voted_up'
    put 'voted_down'    
  end
end

view

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"))

controller

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

UPDATE

Thanks guys for the effort.I did the other way instead of using remote true in the link_to i did in javascript(application.js) .

views

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")

application.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; 

});

The problem is not with the method (put) but that the call itself is remote. That implies an asynchronous call and requires javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM