# Rails render\_to\_string problem

**URL:** <https://www.ruby-forum.com/t/rails-render-to-string-problem/207065>\
**Category:** Rails\
**Created:** [May 17, 2011, 4:13pm UTC](https://www.ruby-forum.com/t/rails-render-to-string-problem/207065 "2011-05-17T16:13:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 17, 2011, 4:13pm UTC](https://www.ruby-forum.com/t/rails-render-to-string-problem/207065/1 "2011-05-17T16:13:16Z")

</div>

Hi  
I`m trying to test my controller with rspec and always get an error.  
The error is in the render\_to\_string method call (without it test  
works fine). So could u please help me with that?

users\_controller.rb:

def update  
@user.update\_attributes!(params[:user])

```
redirect_to @user, :status => 202, :text =>

```

render\_to\_string(:partial =\> “users/show”, :type =\> “json”, :locals =\>  
{:user =\> @user})  
#notice, that redirect\_to was reinitialized and :text is a  
parameter for response\_body  
end  
\_show.tokamak  
user {  
id user.id  
email user.email  
username user.username  
}  
spec file  
it “should NOT update user username” do  
username = @user.username  
put ‘update’, :id =\> @user.id, :user =\> {:username =\> username  
+“abc”}, :format =\> :json  
response.status.should be(202)  
response.headers[“Location”].length.should be \> 0  
puts response.body  
@user.reload  
@user.username.should eq(username)  
end  
end  
So I get an error:

Failure/Error: put ‘update’, :id =\> @user.id, :user =\> {:username =\>  
username+“abc”}, :format =\> :json ActionView::Template::Error: You  
have a nil object when you didn’t expect it! You might have expected  
an instance of Array. The error occurred while evaluating nil.[] # C:/  
Users/makaroni4/free\_frog/ffapi/app/views/users/\_show.tokamak:1:in  
\_app\_views\_users\_\_show\_tokamak\_\_\_509498818 \_32151168\_368311673’ # C:/  
Users/makaroni4/XXX/XXX/app/controllers/users\_controller.rb:22:in  
update’ # ./users\_controller\_spec.rb:34:in `block (4 levels) in ’

So may be I call render\_to\_string method wrong?

---

<div class="post-metadata">

**Author:** ![makaroni4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@makaroni4](https://www.ruby-forum.com/u/makaroni4)\
**Post date:** [May 17, 2011, 8:51pm UTC](https://www.ruby-forum.com/t/rails-render-to-string-problem/207065/2 "2011-05-17T20:51:36Z")

</div>

On Tuesday, May 17, 2011 6:40:26 AM UTC-6, makaroni4 wrote:

> Hi  
> I`m trying to test my controller with rspec and always get an error.  
> The error is in the render\_to\_string method call (without it test  
> works fine). So could u please help me with that?

Does the controller action run fine outside the context of testing?

> parameter for response\_body  
> end

It seems to me the problem may be related specifically to your usage of  
the  
tokamak gem. Haven’t used it myself and don’t know much about it, but  
your  
error message (below) if you read it claims the error happens on line  
one of  
your tokamak-based template.

> +“abc”}, :format =\> :json  
> username+“abc”}, :format =\> :json ActionView::Template::Error: You  
> have a nil object when you didn’t expect it! You might have expected  
> an instance of Array. The error occurred while evaluating nil. # C:/  
> Users/makaroni4/free\_frog/ffapi/app/views/users/\_show.tokamak:1

See how the error is in \_show.tokamak:1? If this controller action works  
outside testing then perhaps the problem is the setup/integration of  
tokamak  
w/rspec. Or, if it doesn’t even work in a normal web request, then  
perhaps  
your \_show.tokamak just has an error (can’t say since its a DSL I know  
nothing about).
