word_list_url
編集: Ruby/Rails がコントローラーで呼び出される関数を探すことを決定した理由を知りたいです。通常、RoR で行われる理由についてはすべて理にかなっていますが、これは私には意味がありません。
respond_with
次のように、コントローラー関数の1つでモデルオブジェクトを作成しようとしています:
def create
@word_list = WordList.new(params[:word_list])
@word_list.key = randomKey
if @word_list.title == nil then @word_list.title = "No Title"
end
if @word_list.words == nil then @word_list.words = "Empty"
end
@word_list.save
respond_with @word_list
end
次に、次のように API を呼び出しています。
curl -v -H "Content-Type: application/json" -X POST -d '{"title":"New Word List", "words":"Words\nFor\nThe\nWord\nList"}' http://localhost:3000/create.json
しかし、私はエラーが発生しています:
NoMethodError (undefined method `word_list_url' for #<WordListsController:0x007fb34c95ca98>):
app/controllers/word_lists_controller.rb:42:in `create'
ただし、これはモデルです。
class WordList < ActiveRecord::Base
attr_accessible :key, :title, :words
end
「メソッドword_list_url
」はどこから取得していますか?何が起こっているのか完全にはわかりません。私は持っていますrespond_to
:
respond_to :json, :xml
ここで何が起こっているのですか?render
の代わりに使用するとrespond_with
、すべて正常に動作します。
現在、他の機能でrespond_with
は、問題なく動作しています。例えば:
def show
lists = WordList.where(:key => params[:id].upcase)
if lists.length > 0 then @word_list = lists.first
elsif numberValue(params[:id]).between?(0, WordList.count) then @word_list = WordList.find(params[:id])
end
respond_with(@word_list)
end
ルート:
create POST /create(.:format) word_lists#create
update PUT /update(.:format) word_lists#update
GET /:id(.:format) word_lists#show