我的 Rails 应用程序中有一个游戏模型。在路由文件中,我为它创建了一个资源路由
resource :games, defaults: {format: :json}
但是,当我使用 Backbone 向 url: 'games' 发出 ajax 请求时,出现 404 错误。运行rake routes
显示“游戏”是一个 POST 请求,并且链接到游戏控制器的创建操作,这显然不是它应该是的(请参阅下面我的问题资源的 rake 路由)。
我还在下面包含了我的游戏模型代码。
谁能解释我做错了什么?
耙路线 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}
相比之下,这里是 Question 模型的路线
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
游戏模型
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