With RubyOnRails
def add
company = params[:person].delete(:company)
@company = Company.find_or_create_by_name(company[:name])
@person = @company.people.create(params[:person])
respond_to do |wants|
wants.html { redirect_to(person_list_url) }
wants.js
wants.xml { render :xml => @person.to_xml(:include => @company) }
end
end
With Camping
class Add
def post
company = input.person.delete(:company)
@company = Company.find_or_create_by_name(company[:name])
@person = @company.people.create(input.person)
case env.ACCEPT
when /text\/html/, /*\/*/
redirect R(PersonList)
when /text\/javascript/
when /application\/xml/
@headers['Content-Type'] = 'application/xml; charset=utf8'
@person.to_xml(:include => @company)
end
end
alias_method :get, :post
end
Notes :
- Not sure Camping's input supports hashes like that
- You have to return the content-type with Camping
- just testing if the Accept header contains a mimetype isn't correct (because it might be listed with preference = 0); consider using mimeparse
- Apache content-negotiation document : http://httpd.apache.org/docs/2.0/en/content-negotiation.html
