[Ajax] link_to remote with method POST or DELETE

Hi! I’m developing a test application, just to experiment a little bit
with ajax. Right now I only have a Company model with the attributes
name:string, description:text and voted:boolean. What I’m trying to do
is: in company#index show a “vote” link to vote a company; after the
company has been voted the link should be replaced by an “unvote” link
to unvote it and so on (after unvoting replace with “vote”, etc.).
From what I have read so far I expect the following code to work just
fine:

app/controllers/companies_controller.rb

class CompaniesController < ApplicationController
before_filter :retrieve_company, :only => [:vote, :unvote]

def index
@companies = Company.all
end

def vote
@company.update_attributes :voted => true
end

def unvote
@company.update_attributes :voted => false
respond_to do |format|
format.js { render “vote” }
end
end

private
def retrieve_company
@company = Company.find(params[:id])
end
end

app/helpers/companies_helper.rb

module CompaniesHelper
def vote_link_for(company)
if company.voted?
link_to “Unvote”, unvote_company_path(company), :id =>
“vote_link”, :remote => true, :method => “delete”
else
link_to “Vote”, vote_company_path(company), :id => “vote_link”,
:remote => true, :method => “post”
end
end
end

app/views/companies/vote.js.erb

$("#vote_link").html("<%= vote_link_for(@company) %>")

app/views/companies/_company.html.haml

%tr
%td= company.name
%td= company.description
%td= vote_link_for(company)

config/routes.rb

[…]
root :to => “companies#index”
resources :companies do
member do
post :vote
delete :unvote
end
end

The problem is that the server receives GET requests instead of POST and
DELETE:

Started GET “/companies/1/vote” for 127.0.0.1 at 2011-03-06 22:27:37
+0100

So I receive a Routing Error because I don’t have any GET route for
:vote and :unvote.
What am I doing wrong?

Thanks for your time and help.

On 6 Mar 2011, at 21:51, Alberto S. [email protected] wrote:

Hi! I’m developing a test application, just to experiment a little bit
with ajax. Right now I only have a Company model with the attributes
name:string, description:text and voted:boolean. What I’m trying to do
is: in company#index show a “vote” link to vote a company; after the
company has been voted the link should be replaced by an “unvote” link
to unvote it and so on (after unvoting replace with “vote”, etc.).
From what I have read so far I expect the following code to work just
fine:

Is your page loading the appropriate version of rails.js

Frederick C. wrote in post #985822:

Is your page loading the appropriate version of rails.js

I think so. I’m using the jquery-ujs one and the produced output of my
page’s head is:

So it looks like everything should be fine.

Did you overwrite rails.js when installing jquery-ujs? (see
https://github.com/rails/jquery-ujs “Installation”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs