Ok, I have a model that is very simple:
ServiceType(id: integer, title: string, duration: integer, created_at: datetime, updated_at: datetime)
Because its so simple, It’s ridiculous that I should have a separate page for creating and updating those types of records. What I would like to do is have a form on my index page which I can use to create new records and if I click the edit link, I would like the form to load that record so that the form doubles as a create/update form.
Has anyone already done this before or does anyone know of a blog article somewhere that shows how to do this?
EDIT: Using the blog link posted below by Laheab I am at this point:
If you are using Rails 3, Laheab’s link is what you want, but BE WARNED!! The guy who wrote it left a ton of obvious errors in his code.. look for bad comment blocks like */ */ instead of /* */ and look for times where he is using jQuery but leaves off the $.
IF you are using Rails 2.3.x, I have posted my modified version of this as an answer.
If you are running Rails 2.3.x, you need to modify what the guy does in this blog link.
This is what I did to get create/edit/destroy(this isn’t working yet, it still renders destroy.js.erb as html for some reason) it to work:
make rails app:
And now add/replace the following files:
public/javascripts/application.js (see Railscasts episode 136 http://railscasts.com/episodes/136-jquery):
app/controllers/posts_controller:
app/views/layouts/posts.html.erb
under app/views/posts delete EVERYTHING and add the following files:
index.html.erb:
index.js.erb:
create.js.erb:
destroy.js.erb:
_posts.erb:
edit.js.erb:
_form.erb:
update.js.erb: