# Simply\_RESTful and Bulk Import URLs

**URL:** <https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725>\
**Category:** Rails\
**Created:** [July 31, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725 "2006-07-31T19:20:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [July 31, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725/1 "2006-07-31T19:20:39Z")

</div>

Hi,

I have a people controller, and want to import bulk import multiple  
people from a CSV file.

In my routes.rb file I have:

map.resource :person

Now, it’s a little unclear to me how to further configure the routes to  
allow me to add the following URLs:

I need a URL to choose a file to upload, so I need an HTML ‘frontend’  
for it, so I figured the following URL fits into the REST/CRUD paradigm:

/people;bulk\_import (using a GET method)

Once I have picked my CSV file, I need to do a POST to perform the  
actual bulk import.

Here I was thinking of either:

/people/;bulk\_import (using a POST method)

or

/people/bulk\_import (using a POST method).

I’m getting a bit confused on how to think about this ‘new’ REST/CRUD  
paradigm, especially in terms of URLs, and then using the simply\_restful  
plugin to manage the named routes.

Can somebody clear this up for me before my head explodes.

Thanks  
Joerg

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [July 31, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725/2 "2006-07-31T20:26:45Z")

</div>

Joerg D. wrote:

> Hi,
> 
> I have a people controller, and want to import bulk import multiple  
> people from a CSV file.
> 
> In my routes.rb file I have:
> 
> map.resource :person
> 
> Now, it’s a little unclear to me how to further configure the routes to  
> allow me to add the following URLs:
> 
> I need a URL to choose a file to upload, so I need an HTML ‘frontend’  
> for it, so I figured the following URL fits into the REST/CRUD paradigm:
> 
> /people;bulk\_import (using a GET method)
> 
> Once I have picked my CSV file, I need to do a POST to perform the  
> actual bulk import.

I’m still trying to figure out this RESTful stuff as well, but my  
understanding is that these complications about the verb ususally mean  
you need a new noun. So rather than “people;bulk\_import” how about

GET bulk\_people;new  
POST bulk\_people

Or perhaps even better is to roll it into your people create action.  
This way it’s still restful, but also supports other ways of creating  
records at the same url based on passed parameters.

#people controller  
def create  
if params[:csv]  
create\_people\_from\_csv(params[:csv])  
else  
Person.create(params[:person])  
end  
end

private  
def create\_people\_from\_csv(csv)  
…parse csv…  
csv.each {|line| Person.create(line)}  
end

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [August 1, 2006, 10:19am UTC](https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725/3 "2006-08-01T10:19:37Z")

</div>

Cool - that’s awesome Jeremy thanks. As simple as that hey.

So to summarize the REST/CRUD way:

1. if you need any extra actions other than the 4 crud verbs, then  
either you need to introduce a new noun into your model, or - in this  
case - where it still makes sense to add these extra actions onto the  
existing people noun, then add them with a ; to the end of the url (eg  
/people;upload). Using the simply\_restful plugin allows you to easily  
create these kind of urls.

2. each action in your controller should respond to only one http  
method.

3. Be reasonable. You don’t have to take things to the extreme.

Joerg

Jeremy K. wrote:

> On Jul 31, 2006, at 10:20 AM, Joerg D. wrote:
> 
> > Now, it’s a little unclear to me how to further configure the  
> > routes to  
> > allow me to add the following URLs:
> > 
> > I need a URL to choose a file to upload, so I need an HTML ‘frontend’  
> > for it, so I figured the following URL fits into the REST/CRUD  
> > paradigm:
> > 
> > /people;bulk\_import (using a GET method)
> 
> # GET /people;upload
> 
> map.resource :person, :collection =\> { :upload =\> :get, :import  
> =\> :post }
> 
> class PeopleController \< ApplicationController  
> # Html form to upload CSV file.  
> def upload  
> end
> 
> ```
> # Handle CSV file upload.
> def import
> Person.create(csv_to_array_of_person_hashes(params[:csv].read)
> end
> 
> ```
> 
> end
> 
> To take it a bit further, register the CSV MIME type
> 
> # register Mime::CSV as ‘text/csv’ with synonym ‘text/c-s-v’
> 
> Mime::Type.register :csv, ‘text/csv’, [‘text/comma-separated-values’]
> 
> so you can respond\_to csv in index, show, etc
> 
> respond\_to do |wants|  
> wants.html # render the rhtml template  
> wants.csv { render :text =\> person.to\_csv }  
> end
> 
> and automatically parse CSV request bodies into @params
> 
> ActionController::Base.param\_parsers[Mime::CSV] = Proc.new do |data|  
> # return a params hash from the csv data (better have a header  
> row…)  
> csv\_to\_person\_hash(data)  
> end
> 
> so you can create and update with CSV as well.
> 
> Best,  
> jeremy

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [July 31, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/simply-restful-and-bulk-import-urls/100725/4 "2006-07-31T21:35:29Z")

</div>

On Jul 31, 2006, at 10:20 AM, Joerg D. wrote:

> Now, it’s a little unclear to me how to further configure the  
> routes to  
> allow me to add the following URLs:
> 
> I need a URL to choose a file to upload, so I need an HTML ‘frontend’  
> for it, so I figured the following URL fits into the REST/CRUD  
> paradigm:
> 
> /people;bulk\_import (using a GET method)

# GET /people;upload

map.resource :person, :collection =\> { :upload =\> :get, :import  
=\> :post }

class PeopleController \< ApplicationController  
# Html form to upload CSV file.  
def upload  
end

```
 # Handle CSV file upload.
 def import
   Person.create(csv_to_array_of_person_hashes(params[:csv].read)
 end

```

end

To take it a bit further, register the CSV MIME type

# register Mime::CSV as ‘text/csv’ with synonym ‘text/c-s-v’

Mime::Type.register :csv, ‘text/csv’, [‘text/comma-separated-values’]

so you can respond\_to csv in index, show, etc

respond\_to do |wants|  
wants.html # render the rhtml template  
wants.csv { render :text =\> person.to\_csv }  
end

and automatically parse CSV request bodies into @params

ActionController::Base.param\_parsers[Mime::CSV] = Proc.new do |data|  
# return a params hash from the csv data (better have a header  
row…)  
csv\_to\_person\_hash(data)  
end

so you can create and update with CSV as well.

Best,  
jeremy
