# Problem with passing to constructor

**URL:** <https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744>\
**Category:** Rails\
**Created:** [August 10, 2007, 11:46am UTC](https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744 "2007-08-10T11:46:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 10, 2007, 11:46am UTC](https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744/1 "2007-08-10T11:46:08Z")

</div>

I think i’m doing something quite simple here but can;t quite get it: I  
had a load of creation logic in my controller that i decided to move to  
the model, under the ‘skinny controller’ principle. So, my controller  
is now passing through the results from a form page, and another  
argument of session[:user], to the model constructor:

#controller method  
def create  
if @article = Article.new params[:article], :user =\> session[:user]  
flash[:notice] = ‘Article was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

#model constructor  
def initialize(params = {})  
super  
if self.title == “”  
self.title =  
(Hpricot(open(@article.url))/“title”).first.inner\_html  
end  
self.added\_at = DateTime.now.to\_s  
self.user\_id = params[:user]  
self.points = 1  
return self.save  
end

But i’m getting an error that looks like a basic syntax error from the  
controller:  
app/controllers/article\_controller.rb:23: syntax error, unexpected  
tIDENTIFIER, expecting kTHEN or ‘:’ or ‘\n’ or ‘;’  
if @article = Article.new params[:article], :user =\> session[:user]

Can anyone see what’s wrong with my controller constructor call?

thanks in advance  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 10, 2007, 11:49am UTC](https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744/2 "2007-08-10T11:49:57Z")

</div>

Max W. wrote:

> if @article = Article.new params[:article], :user =\> session[:user]

Try this:

if @article = Article.new(params[:article], :user =\> session[:user])

Lutz

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 10, 2007, 11:54am UTC](https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744/3 "2007-08-10T11:54:10Z")

</div>

Lutz H. wrote:

> Max W. wrote:
> 
> > if @article = Article.new params[:article], :user =\> session[:user]
> 
> Try this:
> 
> if @article = Article.new(params[:article], :user =\> session[:user])
> 
> Lutz

i actually tried that already, no joy…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 10, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/problem-with-passing-to-constructor/111744/4 "2007-08-10T12:08:52Z")

</div>

> > Try this:
> > 
> > if @article = Article.new(params[:article], :user =\> session[:user])

Actually i just tried this again and it worked this time (buh? sorry),  
but now i’m having problems in the model getting stuff out of the  
params: this line in initialize is having a problem:

```
self.user_id = params[:user]

```

getting a nil object error “while evaluating nil.[]” - it looks like  
params has been lost or something by the time i get to this line…I  
must be passing :user through badly somehow. Do you know why?
