2013-06-21 22 views
0

Я хочу создать новый объект «Изображение» на сервере Rails из json, используя HTTP POST.Как создать новый объект из JSON POST?

Вот код 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 

routes.rb:

MyServer::Application.routes.draw do 

    post 'images' => 'images#create' 

end 

Я использую расширение Chrome "Простой клиент REST". Этого запрос:

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=&gt;[:en], :formats=&gt;[:json], :handlers=&gt;[:erb, :builder, :coffee]}. Searched in: 
     * &quot;c:/Users/XXX/workspaceRubyOnRails/my_server/app/views&quot; 
    </p> 

Запуск рек маршрутов: $ рек маршруты

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 

ответ

1

Попробуйте отправить запрос на http://localhost:3000/images.json вместо http://localhost:3000/images.

+0

К сожалению, все еще «500 Внутренняя ошибка сервера», отсутствует шаблон –

+0

Не могли бы вы вставить маршруты, связанные с изображениями? Сделайте это, выполнив 'rake routes'. – darksky

+0

images POST /images(.:format) images # create –

Смежные вопросы