HTTP POST を使用して JSON から Rails サーバー上に新しい "Image" オブジェクトを作成したいと考えています。
images_controller コードは次のとおりです。
class ImagesController < ApplicationController
respond_to :json
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to images_path }
format.json { render :success => true }
else
format.html { render "new" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end #respond_to
end
end
ルート.rb:
MyServer::Application.routes.draw do
post 'images' => 'images#create'
end
Chrome拡張機能「Simple REST client」を使用しています。これはリクエストです:
URL: http://localhost:3000/images
METHOD: POST
Headers:
Content-Type: application/json
Accept: application/json
Data:
{"image": { "title":"myImage", "url": "someUrl" } }
これは応答です:
Status:500 Internal Server Error
Data:
<title>Action Controller: Exception caught</title>
<h1>Template is missing</h1>
<p>Missing template images/create, application/create with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "c:/Users/XXX/workspaceRubyOnRails/my_server/app/views"
</p>
rake routes の実行: $ rake routes
images_edit GET /images/edit(.:format) images#edit
images_new GET /images/new(.:format) images#new
images GET /images(.:format) images#index
image GET /images/:id(.:format) images#show
images POST /images(.:format) images#create