Reputation: 42069
I have a Game model in my Rails app. In the routes file, I created a resource route for it
resource :games, defaults: {format: :json}
However, when I made an ajax request to url: 'games' using Backbone, I got a 404 error. Running rake routes
shows that 'games' is a POST request, and is linked to the create action of the games controller, which is obviously not what it's supposed to be (see the rake routes for my Question resource below).
I've also included my Game model code below.
Can anyone explain what I've done wrong?
rake routes Game.rb
games POST /games(.:format) games#create {:format=>:json}
new_games GET /games/new(.:format) games#new {:format=>:json}
edit_games GET /games/edit(.:format) games#edit {:format=>:json}
GET /games(.:format) games#show {:format=>:json}
PUT /games(.:format) games#update {:format=>:json}
DELETE /games(.:format) games#destroy {:format=>:json}
By comparison, here are the routes for the Question model
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
Game model
class Game < ActiveRecord::Base
attr_accessible :creator_id, :name
has_many :results
has_many :users, :through => :results
has_reputation :votes, source: :user, aggregated_by: :sum #for Active_record_reputation gem
class << self
def win?(chars_left, incorrect_guesses)
chars_left == 0 and incorrect_guesses < 6
end
def correct_response?(correctanswer, guess)
correctanswer == guess
end
def correct_guess?(char_clicked, final_word)
puts char_clicked
puts final_word =~ /#{char_clicked}/i
if final_word =~ /#{char_clicked}/i
true
else
false
end
end
end
end
Upvotes: 0
Views: 77
Reputation: 4880
Using resource :games
won't give you an index
route as it's a singular resource, you need to change it to resources :games
, have a look here.
Upvotes: 2