# Insert data into a database without user input

**URL:** <https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978>\
**Category:** Rails\
**Created:** [July 1, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978 "2010-07-01T21:23:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [July 1, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978/1 "2010-07-01T21:23:14Z")

</div>

Hello there,  
I am new around here and have been looking over the internet for a  
solution but can’t find one. I am fair new to rails but have a decent  
understanding of it to get by and a few years experience behind me.  
My problem is that I try to insert data in a database and it does not  
come up. The information the user types is inserted however I wish to  
insert the ID and other stuff the user can not change.  
This is what I have so far:

The controller:

def create  
@postform = new(params[:postform])  
@post = Post.new(:title =\> @postform.title, :body =\> @postform.body,  
:user\_id =\> session[:user\_id]).save  
respond\_to do |format|  
if @post.save  
format.html { redirect\_to(@post, :notice =\> ‘Post was  
successfully created.’) }  
format.xml { render :xml =\> @post, :status =\> :created,  
:location =\> @post }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @post.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

View:

# New post

\<%= form\_tag :action=\>‘create’ %\>  
Title: \<%= text\_field(“postform”, “title”,:size=\>“20” ) %\>  
  
  
Body: \<%= text\_area(“postform”, “body”,:size=\>“20” ) %\>  
  
  
\<%= label(“postform”, “user\_id”) %\>

\<%= link\_to ‘Back’, posts\_path %\>

Error:

ArgumentError in PostsController#create

wrong number of arguments (1 for 0)

RAILS\_ROOT: C:/Users/Jack/Documents/NetBeansProjects/BlogWithUser

Request

Parameters:

{“authenticity\_token”=\>“ubIO5KeB3WlLD+3hSoLBk4LJSuopsKrgureY6Uq4Rl0=”,  
“postform”=\>{“title”=\>“adsf”,  
“body”=\>“adsf”}}

Thank you for your time  
Jack  
P.S. I tried with the standerd scaffold and that did not work ether (for  
what I want to do)

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [July 1, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978/2 "2010-07-01T21:46:42Z")

</div>

On Thu, 2010-07-01 at 21:23 +0200, Jack Fall wrote:

> ```
> else
> format.html { render :action => "new" }
> format.xml { render :xml => @post.errors, :status =>
> 
> ```
> 
> :unprocessable\_entity }  
> end  
> end  
> end

* * *

def create  
@post = Post.new(params[:postform])  
@post.user\_id = session[:user\_id] #assuming you’ve already  
#got this worked out  
if @post.save  
put your stuff here  
end  
end

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [July 1, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978/3 "2010-07-01T21:53:58Z")

</div>

Craig W. wrote:

> On Thu, 2010-07-01 at 21:23 +0200, Jack Fall wrote:
> 
> > ```
> > else
> > format.html { render :action => "new" }
> > format.xml { render :xml => @post.errors, :status =>
> > 
> > ```
> > 
> > :unprocessable\_entity }  
> > end  
> > end  
> > end
> 
> * * *
> 
> def create  
> @post = Post.new(params[:postform])  
> @post.user\_id = session[:user\_id] #assuming you’ve already  
> #got this worked out  
> if @post.save  
> put your stuff here  
> end  
> end
> 
> –  
> This message has been scanned for viruses and  
> dangerous content by MailScanner, and is  
> believed to be clean.

Craig Walker if I knew you I would kiss you (hypothetical), I never knew  
it would be so simple however thank you very much.  
Robert W. to be truthful I have not got the fogest what I wrote :S,  
I looked back over it and thought “yer this is not going to work in a  
million years”.

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [July 1, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/insert-data-into-a-database-without-user-input/190978/4 "2010-07-01T21:48:42Z")

</div>

Jack Deadcore wrote:

> Hello there,  
> I am new around here and have been looking over the internet for a  
> solution but can’t find one. I am fair new to rails but have a decent  
> understanding of it to get by and a few years experience behind me.  
> My problem is that I try to insert data in a database and it does not  
> come up. The information the user types is inserted however I wish to  
> insert the ID and other stuff the user can not change.  
> This is what I have so far:
> 
> The controller:
> 
> def create  
> @postform = new(params[:postform])

What is this and what do you expect from it? Do you have a “new” method  
that takes one argument in your controller class? Also, the “new” method  
of a controller simply renders a blank form to the browser so that the  
user can fill it out.

> Error:
> 
> ArgumentError in PostsController#create
> 
> wrong number of arguments (1 for 0)

Typically a controller contains a new method (action) that takes 0  
arguments, hence the above error.

> ```
> @post = Post.new(:title => @postform.title, :body => @postform.body,
> 
> ```
> 
> :user\_id =\> session[:user\_id]).save

P.S. Consider this instead:

@post = Post.create(:title =\> @postform.title, :body =\> @postform.body,  
:user\_id =\> session[:user\_id])
