# Can't convert nil into String

**URL:** <https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900>\
**Category:** Rails\
**Created:** [July 13, 2008, 2:24am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900 "2008-07-13T02:24:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![central\_tex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@central\_tex](https://www.ruby-forum.com/u/central_tex)\
**Post date:** [July 13, 2008, 2:24am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900/1 "2008-07-13T02:24:53Z")

</div>

I have tried to get through this error with no luck. In a form I have a  
user enter a title and then search the database for any similar titles.  
I need help with this. It’s like the information isn’t being passed  
through. Here is my information. I am using Rails 2.1 with  
nifty\_scaffold. First I changed my route

routes.rb  
map.resources :titles, :collection =\> { :find\_similar =\> :get }

new.html.erb  
\<% title “New Title” %\>

\<% form\_for @title, :url =\> { :action =\> :find\_similar } do |f| %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<%= f.submit "Search" %\>

\<% end %\>

\<%= link\_to "Back to List", titles\_path %\>

titles\_controller.rb  
def find\_similar  
@searchkey = params[:name]  
@title = Title.find(:all,  
:conditions =\> [“name like ?”,  
‘%’+@searchkey+’%’])  
end

Parameters:

{“commit”=\>“Search”,  
“title”=\>{“name”=\>“one day”},  
“authenticity\_token”=\>“ec64448f9d39d29dd3dbafd1a1a31415ea165bdb”}

TypeError in TitlesController#find\_similar  
can’t convert nil into String  
app/controllers/titles\_controller.rb:48:in `+' app/controllers/titles_controller.rb:48:in`find\_similar’

---

<div class="post-metadata">

**Author:** ![central\_tex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@central\_tex](https://www.ruby-forum.com/u/central_tex)\
**Post date:** [July 13, 2008, 2:32am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900/2 "2008-07-13T02:32:07Z")

</div>

Hi Quincy,

have you tried the following?

def find\_similar  
@searchkey = params[:title][:name]  
…  
end

On Jul 12, 8:24 pm, Quincy Central\_tex \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![central\_tex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@central\_tex](https://www.ruby-forum.com/u/central_tex)\
**Post date:** [July 13, 2008, 5:51am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900/3 "2008-07-13T05:51:51Z")

</div>

gsterndale wrote:

> Hi Quincy,
> 
> have you tried the following?
> 
> def find\_similar  
> @searchkey = params[:title][:name]  
> …  
> end
> 
> On Jul 12, 8:24 pm, Quincy Central\_tex \<rails-mailing-l…@andreas-

That did it! Thanks gsterndale! Please explain whats happening here. I  
haven’t seen this in any documentation.

---

<div class="post-metadata">

**Author:** ![central\_tex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@central\_tex](https://www.ruby-forum.com/u/central_tex)\
**Post date:** [July 13, 2008, 6:59am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900/4 "2008-07-13T06:59:20Z")

</div>

If you look at the HTML generated by your new.html.erb template you’ll  
see how form\_for names your input fields. For example:  
\<% form\_for @title, :url =\> { :action =\> :find\_similar } do |f| %\>  
\<%= f.text\_field :name %\>  
\<% end %\>

would generate something like:

Note the brackets [] around “name”. As you know, Rails puts the  
submitted form field names and values into a Hash called “params”.  
When a form field with brackets is submitted, Rails interprets the  
value of the parameter “title” as a Hash with a key called “name”. The  
value of that field is assigned to that key. The result is a nested  
Hash, exactly what you pasted in your first post.

{  
“commit”=\>“Search”,  
“title”=\>{  
“name”=\>“one day”  
},  
“authenticity\_token”=\>“ec64448f9d39d29dd3dbafd1a1a31415ea165bdb”  
}

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001737)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

On Jul 12, 11:51 pm, Quincy C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![central\_tex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@central\_tex](https://www.ruby-forum.com/u/central_tex)\
**Post date:** [July 13, 2008, 7:59am UTC](https://www.ruby-forum.com/t/cant-convert-nil-into-string/142900/5 "2008-07-13T07:59:36Z")

</div>

Got it!! thanks
