These railscast  and  show how to do what I think you want,
however they are a bit out of date now. I see there are revised
episodes but to get that you have to pay
Start by looking at those to get the basic idea.
Does anyone know of more up to date examples?