我的Rails应用程序中有一个游戏模型。在routes文件中,我为它创建了一个资源路由
resource :games, defaults: {format: :json}
然而,当我使用Backbone向url:'games'发出ajax请求时,我收到了404错误。运行rake routes
表示'游戏'是一个POST请求,并且链接到游戏控制器的创建动作,这显然不是它应该是的(参见下面我的问题资源的rake路线)。
我还在下面提供了我的游戏模型代码。
谁能解释我做错了什么?
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}
相比之下,这里是问题模型的路线
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
答案 0 :(得分:2)
使用resource :games
不会为您提供index
路由,因为它是一个独特的资源,您需要将其更改为resources :games
,have a look here。