1

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
4

1 に答える 1

1

http://api.rubyonrails.org/classes/ActionController/Responder.htmlを見ると、xml 形式のサポートが次のように拡張されていることがわかります。

format.xml { render :xml => @word_list, :status => :created, :location => @word_list }

私はあなたのエラーを引き起こす可能性のある:location使用を考えています。url_for試す

respond_with @word_list do |format|
  format.xml { render :xml => @word_list, :status => :created }
end
于 2013-03-08T00:27:10.317 に答える