Friday 15 July 2011

ruby on rails - How can I create a new object from a JSON POST? -


I want to create a new "image" object on the Rail Server from Jason via HTML Post.

Here is the controller code for images:

  Classroom Controller & lt; ApplicationController response_to: make json def @image = Image.new (params [: image]) response_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-of-end # post = end-of-end   

routes.rb :

  MySpore :: Application.routes.draw Post 'images' = & gt; End of 'Creating Images #'   

I'm using Chrome Extensions "Simple Rest Customer". This request:

  URL: http: // localhost: 3000 / images Method: Post Header: Content-Type: Application / Jason Accept: Application / Jason Data: {"image": { This is the response:  
  Status: 500 Internal Server Error Data: "myimage", "url": "someUrl"}}   

& Lt; Title & gt; Action Controller: Exception Occurred & lt; / Title & gt; & Lt; H1 & gt; Template is unavailable & lt; / H1> & Lt; P & gt; Missing template image / create, app / create with: Locale = & amp; G; [: N],: format = & amp; Gt; [: Json],: Handlers = & amp; Gt; [: Erb,: builder,: coffee]}. Find in: * & amp; Quot; C: / user / XXX / workspace RubyOnRails / my_server / app / view & amp; Quot; & Lt; / P & gt;

Run the running routes: $ rake path

  images_edit GET /images/edit(.:format) Images # Edit Images /See GET / images / New (.: Format) Images # Get new images / images (.: Format) Images # Index Image Received / Images / :( (. Format:) Images # Show Images POST /images(.:format) Create Images # / Code> <
  

http: // localhost: 3000 / images.json http: // localhost: 3000 / images instead.

No comments:

Post a Comment