creating new records using REST and AJAX
I created a simple application in 开发者_如何学CRails for storing the names of cars:
script/generate scaffold car name:string
I'm looking to create an application which will connect to this using REST and AJAX to create new car names. However, I want this application to be separate from the application which I created in Rails to actually hold the car names, and I don't want to write it in Rails. I just want to create it using plan old HTML and Javascript. How would I write the Javascript code for this?
On your Rails applcation, try running:
rake routes
and that's basically the REST API that's available by running the scaffold generator.
For example:
car GET /cars/:id(.:format) {:controller=>"cars", :action=>"show"}
Maps to this url:
http://localhost:3000/cars/1.xml
Will return the Car model object with an ID of 1 in XML format (with the default scaffold generator)
That API is available from anywhere that can access your application via http. If you want to write some javascript outside rails, you can use one of the frameworks out there or write it yourself (but, this is more advanced and difficult)
The prototype way to make an AJAX call is:
new Ajax.Request(url[, options])
And the jQuery way is:
jQuery.ajax( options )
You'll have to read through the documentation to get your javascript working the way you want.
精彩评论