# Insert data to the databse

**URL:** <https://www.ruby-forum.com/t/insert-data-to-the-databse/141197>\
**Category:** Rails\
**Created:** [June 23, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/insert-data-to-the-databse/141197 "2008-06-23T14:26:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 23, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/insert-data-to-the-databse/141197/1 "2008-06-23T14:26:19Z")

</div>

how can i insert data to the database from any controller

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 23, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/insert-data-to-the-databse/141197/2 "2008-06-23T18:07:27Z")

</div>

Assuming you want to add a user from an arbitrary controller… You  
would instantiate the new user in an action (‘new\_user’) and save it  
to the database (and raise validation errors) from a create action  
(here called ‘create\_user’)

class ArbitraryController \< ActionController::Base

def index  
@users = User.find(:all)  
end

def new\_user  
@user = User.new  
end

def create\_user  
@user = User.new(params[:user])  
@user.save  
redirect\_to :action =\> ‘index’  
end

end

You would have to define the routes in your routes.rb file like:

map.connect ‘/user/new’, :controller =\> ‘arbitrary’, :action =\>  
‘new\_user’

HTH
