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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-06 21:54 UTC
Re: [Ajax] link_to remote with method POST or DELETE
On 6 Mar 2011, at 21:51, Alberto Santini <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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> > ## 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alberto Santini
2011-Mar-06 22:05 UTC
Re: [Ajax] link_to remote with method POST or DELETE
Frederick Cheung wrote in post #985822:> Is your page loading the appropriate version of rails.jsI think so. I''m using the jquery-ujs one and the produced output of my page''s head is: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script> <script src="/javascripts/rails.js?1299441487" type="text/javascript"></script> <meta name="csrf-param" content="authenticity_token"/> <meta name="csrf-token" content="mkOZ4niIW7oNzBZuuc11VWzdsO21HINFpjLg+EyPUOY="/> So it looks like everything should be fine. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Did you overwrite rails.js when installing jquery-ujs? (see https://github.com/rails/jquery-ujs "Installation") On Mar 6, 5:05 pm, Alberto Santini <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung 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: > > <script > src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" > type="text/javascript"></script> > <script src="/javascripts/rails.js?1299441487" > type="text/javascript"></script> > <meta name="csrf-param" content="authenticity_token"/> > <meta name="csrf-token" > content="mkOZ4niIW7oNzBZuuc11VWzdsO21HINFpjLg+EyPUOY="/> > > So it looks like everything should be fine. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.