SinatraアプリのコードをRailsコンテキストで機能させようとしています。Sinatraアプリは、ajaxリクエストを使用してSinatraルート/コントローラーアクションをトリガーします。たとえば、JavaScriptモデルで新しい関数をトリガーした場合
new: function() {
var _this = this;
$.ajax({
url: "/gamestart",
type: "POST",
....
Sinatraアプリでルート/コントローラーコードをトリガーします
post "/new" do
end
Railsでこれを機能させようとすると、500の内部サーバーエラーが発生します。私のRailsアプリでは、new_gameボタンがRailsルートへのajaxリクエストをトリガーし、コントローラーアクションをトリガーします。そのコントローラーアクションは、Railsモデルを使用してデータベースからデータを取得します。何らかの理由で、Railsでそれを行うのは正しい方法ではないようですが、それがサーバーエラーが発生する理由であるかどうか疑問に思っています
GET http://localhost:3000/gamestart 500 (Internal Server Error)
可能であれば、以下に概説する一連のアクションのどこでエラーが発生しているのか、そしてそれを修正するために私が何をする可能性があるのかを教えてください。
1新しいゲームボタンをクリックすると、「startNewGame」メソッドがトリガーされます
'click #new_game': 'startNewGame',
2 startNewGameメソッドは、ゲームモデルのメソッドを呼び出します
startNewGame: function() {
this.model.new();
},
3ゲームモデルの新しいメソッドは、URL'/gamestart'に対してGETリクエストを行います。投稿リクエストも試しました。なぜPOSTリクエストである必要があるのかわかりませんが、どちらも機能しませんでした。(元のSinatraアプリケーションでは、gamestartのURLはすぐに関数post'/ gamestart'に導かれました...)
new: function() {
var _this = this;
$.ajax({
url: "/gamestart",
type: "GET", \\\ also tried POST
success: function(response) {
var json = $.parseJSON(response);
_this.set({lost: false});
_this.set({win: false});
_this.trigger("gameStartedEvent", json);
}
})
},
4URLをRailsルーターファイルのコントローラーアクションに転送しました
match 'gamestart' => 'locations#gamestart', :via => :get
元のSinatraアプリケーションでは、ルートとコントローラーアクションが組み合わされていたことに注意してください
5locations_controller.rbのgamestartメソッド
def gamestart
word = Word.get_random
masquerade_word = Word.masquerade(word)
session[:word] = word
session[:incorrect_guesses] = 0
session[:chars_left] = word.size
session[:revealed_word] = masquerade_word
{:word => masquerade_word}.to_json
end
6ロケーションコントローラから呼び出されるワードモデルWord.rbのget_randomメソッド
def get_random
words = []
locations = Location.all (this pulls up the names of the locations from the db)
locations.each do |e|
words << e.name
end
words.sample
end
エラーメッセージ
GET http://localhost:3000/gamestart 500 (Internal Server Error) jquery.js:8215
XHR finished loading: "http://localhost:3000/gamestart". jquery.js:8215
send jquery.js:8215
jQuery.extend.ajax jquery.js:7767
window.Game.Backbone.Model.extend game.js:27
window.OptionsView.Backbone.View.extend.startNewGame optionsView.js:14
jQuery.event.dispatch jquery.js:3062
elemData.handle.eventHandle
元のSinatraアプリケーションでは、ルートとコントローラーアクションが通常のSinatraの方法で組み合わされていることに注意してください。
post "/gamestart" do
word = Word.get_random
masquerade_word = Word.masquerade(word)
session[:word] = word
session[:incorrect_guesses] = 0
session[:chars_left] = word.size
session[:revealed_word] = masquerade_word
{:word => masquerade_word}.to_json
end
UPDATE 500エラーは、テンプレートが見つからないことが原因のようです。ロケーションコントローラのこのメソッドは何もレンダリングしていませんでした。ビューファイルがありませんでした。したがって、コントローラーをrespond_to:jsonに変更し、アクションの最後にrespond_withを使用しましたが、それによって406エラーが発生しました。
def gamestart
word = Word.get_random
masquerade_word = Word.masquerade(word)
session[:word] = word
session[:incorrect_guesses] = 0
session[:chars_left] = word.size
session[:revealed_word] = masquerade_word
{:word => masquerade_word}.to_json
end
406エラーをトリガーするようになりました
respond_to :json
def gamestart
word = Word.get_random
masquerade_word = Word.masquerade(word)
session[:word] = word
session[:incorrect_guesses] = 0
session[:chars_left] = word.size
session[:revealed_word] = masquerade_word
plainvariable = {:word => masquerade_word}.to_json ###changed
respond_with plainvariable ###changed
end