ruby - Ajax on Rails, not clear on Rails versions -


i following michael hartl's excellent tutorial on ruby on rails, in particular 1 available in website( rails version 3.2 ).

i @ point (section 11.2.5 specifically), "follow" , "unfollow" action implemented ajax request. have 2 versions of application code, 1 works, other 1 not. in other works, got working using other syntax, wondering how or rather why.

this 1 one found in website:

class relationshipscontroller < applicationcontroller   before_filter :signed_in_user    def create     @user = user.find(params[:relationship][:followed_id])     current_user.follow!(@user)     respond_to |format|       format.html { redirect_to @user }       format.js  { redirect_to @user }   #as suggested carolclarinet     end   end    def destroy     @user = relationship.find(params[:id]).followed     current_user.unfollow!(@user)     respond_to |format|       format.html { redirect_to @user }       format.js  { redirect_to @user }   #as suggested carolclarinet     end   end end 

and in case does not work.

however other implementation( found in tutorial's official page: https://github.com/railstutorial/sample_app_2nd_ed) of same functionality does work me:

class relationshipscontroller < applicationcontroller   before_filter :signed_in_user    respond_to :html, :js    def create     @user = user.find(params[:relationship][:followed_id])     current_user.follow!(@user)     respond_with @user   end    def destroy     @user = relationship.find(params[:id]).followed     current_user.unfollow!(@user)     respond_with @user   end end 

this gemfile in tutorial's github page:

source 'https://rubygems.org'  gem 'rails', '3.2.14' gem 'bootstrap-sass', '2.1' gem 'bcrypt-ruby', '3.0.1' gem 'faker', '1.0.1' gem 'will_paginate', '3.0.3' gem 'bootstrap-will_paginate', '0.0.6' gem 'jquery-rails', '2.0.2'  group :development, :test   gem 'sqlite3', '1.3.5'   gem 'rspec-rails', '2.11.0'   gem 'guard-rspec', '1.2.1'   gem 'guard-spork', '1.2.0'   gem 'childprocess', '0.3.6'   gem 'spork', '0.9.2' end  # gems used assets , not required # in production environments default. group :assets   gem 'sass-rails', '3.2.5'   gem 'coffee-rails', '3.2.2'   gem 'uglifier', '1.2.3' end  group :test   gem 'capybara', '1.1.2'   gem 'factory_girl_rails', '4.1.0'   gem 'cucumber-rails', '1.2.1', :require => false   gem 'database_cleaner', '0.7.0'   # gem 'launchy', '2.1.0'   # gem 'rb-fsevent', '0.9.1', :require => false   # gem 'growl', '1.0.3' end  group :production   gem 'pg', '0.12.2' end 

and `gemfile:

source 'https://rubygems.org'  gem 'rails', '3.2.11' gem 'bootstrap-sass', '2.0.0' gem 'bcrypt-ruby','3.0.1' gem 'faker','1.0.1' gem 'will_paginate','3.0.3' gem 'bootstrap-will_paginate','0.0.6' # bundle edge rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git'  group :development     gem 'sqlite3', '1.3.5'     gem 'annotate', '~> 2.4.1.beta' end # gems used assets , not required # in production environments default. group :assets     gem 'sass-rails', '3.2.4'     gem 'coffee-rails', '3.2.2'     gem 'uglifier', '1.2.3' end  gem 'jquery-rails', '2.0.2'  group :test, :development     gem 'rspec-rails', '2.10.0'     gem 'guard-rspec', '0.5.5'     gem 'guard-spork', '0.3.2'     gem 'spork', '0.9.0' end   group :test     gem 'capybara', '1.1.2'     gem 'factory_girl_rails', '1.4.0'     gem 'cucumber-rails', '1.2.1', require: false     gem 'database_cleaner', '0.7.0'     #gem 'shoulda-matchers'     gem 'launchy' end   group :production     gem 'pg','0.12.2' end   #gem 'sqlite3' # # ## gems used assets , not required ## in production environments default. #group :assets #  gem 'sass-rails',   '~> 3.2.3' #  gem 'coffee-rails', '~> 3.2.1' # #  # see https://github.com/sstephenson/execjs#readme more supported runtimes #  # gem 'therubyracer', :platforms => :ruby # #  gem 'uglifier', '>= 1.0.3' #end   # use activemodel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0'  # use jbuilder templates json # gem 'jbuilder'  # use unicorn app server # gem 'unicorn'  # deploy capistrano # gem 'capistrano'  # use debugger # gem 'debugger' 

i assume between these 2 versions (3.2.11 3.2.14) there significant syntax change?

this error encounter when i'm using 3.2.11 non-working version:

failure/error: before { click_button "follow" }      actioncontroller::routingerror:        no route matches [get] "/relationships"      # (eval):2:in `click_button'      # ./spec/requests/user_pages_spec.rb:113:in `block (6 levels) in <top  (required)>' 

edit 1

this config/routes.rb, there shouldn't get /relationships request, should it?:

sampleapp::application.routes.draw     resources :users         member             :following, :followers         end     end     resources :sessions, only: [:new, :create, :destroy]     resources :microposts, only: [:create, :destroy]     resources :relationships, only:[:create,:destroy]   #get "users/new"   # "static_pages/home"  # "static_pages/help"  # "static_pages/about"  # "static_pages/contact"    root to: 'static_pages#home'    match '/signup', to: 'users#new'   match '/signin', to: 'sessions#new'   match '/signout', to: 'sessions#destroy', via: :delete    match '/help', to: 'static_pages#help'   match '/about', to: 'static_pages#about'   match '/contact', to: 'static_pages#contact'    # priority based upon order of creation:   # first created -> highest priority.    # sample of regular route:   #   match 'products/:id' => 'catalog#view'   # keep in mind can assign values other :controller , :action    # sample of named route:   #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase   # route can invoked purchase_url(:id => product.id)    # sample resource route (maps http verbs controller actions automatically):   #   resources :products    # sample resource route options:   #   resources :products   #     member   #       'short'   #       post 'toggle'   #     end   #   #     collection   #       'sold'   #     end   #   end    # sample resource route sub-resources:   #   resources :products   #     resources :comments, :sales   #     resource :seller   #   end    # sample resource route more complex sub-resources   #   resources :products   #     resources :comments   #     resources :sales   #       'recent', :on => :collection   #     end   #   end    # sample resource route within namespace:   #   namespace :admin   #     # directs /admin/products/* admin::productscontroller   #     # (app/controllers/admin/products_controller.rb)   #     resources :products   #   end    # can have root of site routed "root"   # remember delete public/index.html.   # root :to => 'welcome#index'    # see how routes lay out "rake routes"    # legacy wild controller route that's not recommended restful applications.   # note: route make actions in every controller accessible via requests.   # match ':controller(/:action(/:id))(.:format)' end 

and output of $rake routes:

following_user    /users/:id/following(.:format) users#following followers_user    /users/:id/followers(.:format) users#followers          users    /users(.:format)               users#index                post   /users(.:format)               users#create       new_user    /users/new(.:format)           users#new      edit_user    /users/:id/edit(.:format)      users#edit           user    /users/:id(.:format)           users#show                put    /users/:id(.:format)           users#update                delete /users/:id(.:format)           users#destroy       sessions post   /sessions(.:format)            sessions#create    new_session    /sessions/new(.:format)        sessions#new        session delete /sessions/:id(.:format)        sessions#destroy     microposts post   /microposts(.:format)          microposts#create      micropost delete /microposts/:id(.:format)      microposts#destroy  relationships post   /relationships(.:format)       relationships#create   relationship delete /relationships/:id(.:format)   relationships#destroy           root        /                              static_pages#home         signup        /signup(.:format)              users#new         signin        /signin(.:format)              sessions#new        signout delete /signout(.:format)             sessions#destroy                  /help(.:format)                static_pages#help                 /about(.:format)               static_pages#about        contact        /contact(.:format)             static_pages#contact 

there no get /relationships. yet routing still looking it.

this debugging of rails server when click on "follow" button, i.e. when post /relationships, calls relationship#create:

started post "/relationships" 127.0.0.1 @ 2013-10-07 16:41:14 +0200                                                                        [2/649] processing relationshipscontroller#create js   parameters: {"utf8"=>"✓", "authenticity_token"=>"4auiemlg0nnfnarcvmu9mc7e62hysfxxhyfyq9eeuxs=", "relationship"=>{"followed_id"=>"95"}, "commit"=>"fol low"}   user load (0.9ms)  select "users".* "users" "users"."remember_token" = 'sjwczvhqgp1chbw2uovw4q' limit 1   user load (0.6ms)  select "users".* "users" "users"."id" = ? limit 1  [["id", "95"]]    (0.0ms)  begin transaction   sql (75.3ms)  insert "relationships" ("created_at", "followed_id", "follower_id", "updated_at") values (?, ?, ?, ?)  [["created_at", mon, 07 oct  2013 14:41:14 utc +00:00], ["followed_id", 95], ["follower_id", 1], ["updated_at", mon, 07 oct 2013 14:41:14 utc +00:00]]    (266.8ms)  commit transaction redirected http://127.0.0.1:3000/relationships completed 302 found in 353ms (activerecord: 343.7ms)   started "/relationships" 127.0.0.1 @ 2013-10-07 16:41:15 +0200  actioncontroller::routingerror (no route matches [get] "/relationships"):   actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'   actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'   railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'   railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'   activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'   railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'   actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'   rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'   rack (1.4.5) lib/rack/runtime.rb:17:in `call'   activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'   rack (1.4.5) lib/rack/lock.rb:15:in `call'   actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'   railties (3.2.11) lib/rails/engine.rb:479:in `call'   railties (3.2.11) lib/rails/application.rb:223:in `call'   rack (1.4.5) lib/rack/content_length.rb:14:in `call'   railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'   rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'   /home/toni/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'   /home/toni/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'   /home/toni/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'     rendered /home/toni/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms) 

no route matches [get] "/relationships 

you appear sending get request when in routes file you've declared apost request. double check that. create methods typically post. may need change form action and/or change ajax request type.

for future reference, setting respond_to(:html, :js) whole controller same using respond_to block in method. view according how request came in (i.e, js -> create.js.erb, html -> create.html.erb )


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -