# Can't convert Symbol into String

**URL:** <https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459>\
**Category:** Ruby\
**Created:** [February 14, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459 "2010-02-14T22:10:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 14, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/1 "2010-02-14T22:10:47Z")

</div>

I’m getting the following error

* * *

TypeError in Users#new

Showing app/views/users/new.html.erb where line #20 raised:

can’t convert Symbol into String

Extracted source (around line #20):

17:

  
18: 

\<%= shnView.label(‘nickname’) %\> _(e.g.  
Craig10027)_  
  
19: \<%= f.text\_field :nickname %\>

  
20: 

\<%= shnView.label(:email) %\> _(e.g. Craig10027)_  
_(e.g. removed\_email\_address@domain.invalid)_  
  
21: \<%= f.text\_field :email %\>

  
22: 

\<%= f.label :password %\>  
  
23: \<%= f.password\_field :password %\>

* * *

Why can’t my shnView.label(:email) method create a symbol to sting  
conversion … but f.text\_field can?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 14, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/2 "2010-02-14T22:46:00Z")

</div>

Ralph S. wrote:

> I’m getting the following error
> 
> * * *
> 
> TypeError in Users#new
> 
> Showing app/views/users/new.html.erb where line #20 raised:

This looks like you are working with Rails. Rails is not Ruby. Ruby is a  
programming language; Rails is a web application framework which happens  
to be written _in_ Ruby.

If you’re having a problem with Rails, your best bet is to ask on a  
Rails mailing list or forum.

> Why can’t my shnView.label(:email) method create a symbol to sting  
> conversion … but f.text\_field can?

Without seeing the code to your shnView.label method, I couldn’t say.  
But since you used shnView.label(‘nickname’) successfully above, maybe  
shnView.label(‘email’) is what you want.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 14, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/3 "2010-02-14T22:55:17Z")

</div>

Sunday, February 14, 2010, 2:46:02 PM, you wrote:

BC\> Ralph S. wrote:

> > I’m getting the following error

> > * * *

> > TypeError in Users#new

> > Showing app/views/users/new.html.erb where line #20 raised:

BC\> This looks like you are working with Rails. Rails is not Ruby. Ruby  
is a  
BC\> programming language; Rails is a web application framework which  
happens  
BC\> to be written _in_ Ruby.

BC\> If you’re having a problem with Rails, your best bet is to ask on a  
BC\> Rails mailing list or forum.

> > Why can’t my shnView.label(:email) method create a symbol to sting  
> > conversion … but f.text\_field can?

BC\> Without seeing the code to your shnView.label method, I couldn’t  
say.  
BC\> But since you used shnView.label(‘nickname’) successfully above,  
maybe  
BC\> shnView.label(‘email’) is what you want.

BC\> Regards,

BC\> Brian.

Brian,

I posted the question here because I am asking a Ruby question.

Let me rephrase the question …

Under what circumstances will Ruby not be able to convert a symbol into  
a string?

Ralph

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 14, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/4 "2010-02-14T23:39:42Z")

</div>

Ralph S. wrote:

> Sunday, February 14, 2010, 2:46:02 PM, you wrote:
> 
> …
> 
> > > Why can’t my shnView.label(:email) method create a symbol to sting  
> > > conversion … but f.text\_field can?

That answers to your problem: it’s a Rails matter. Likely, text\_field  
implements the chance of passing String type arguments, label not.

> I posted the question here because I am asking a Ruby question.
> 
> Let me rephrase the question …
> 
> Under what circumstances will Ruby not be able to convert a symbol into  
> a string?
> 
> Ralph

As above.

Bye

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 15, 2010, 12:26am UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/5 "2010-02-15T00:26:30Z")

</div>

Ralph S. wrote:

> Sunday, February 14, 2010, 2:46:02 PM, you wrote:
> 
> BC\> Ralph S. wrote:
> 
> > > I’m getting the following error
> 
> > > * * *
> 
> > > TypeError in Users#new
> 
> > > Showing app/views/users/new.html.erb where line #20 raised:
> 
> I posted the question here because I am asking a Ruby question.
> 
> Let me rephrase the question …
> 
> Under what circumstances will Ruby not be able to convert a symbol into  
> a string?
> 
> Ralph

When the method in question is expecting a String and not a Symbol (as  
in, its most likely not calling to\_s in the argument in question). I  
don’t know what shnView view is returning, nor what sort of input its  
return value’s “label” method is expecting, but why not just pass  
“email” to it? Alternatively, yeah, post this (possibly on the Rails  
list) with more relatively info.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 15, 2010, 4:49am UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/6 "2010-02-15T04:49:19Z")

</div>

Sunday, February 14, 2010, 4:26:32 PM, you wrote:

PH\> Ralph S. wrote:

> > Sunday, February 14, 2010, 2:46:02 PM, you wrote:

> > BC\> Ralph S. wrote:
> > 
> > > > I’m getting the following error

> > > > * * *

> > > > TypeError in Users#new

> > > > Showing app/views/users/new.html.erb where line #20 raised:

> > I posted the question here because I am asking a Ruby question.

> > Let me rephrase the question …

> > Under what circumstances will Ruby not be able to convert a symbol into  
> > a string?

> > Ralph

PH\> When the method in question is expecting a String and not a Symbol  
(as  
PH\> in, its most likely not calling to\_s in the argument in question). I  
PH\> don’t know what shnView view is returning, nor what sort of input  
its  
PH\> return value’s “label” method is expecting, but why not just pass  
PH\> “email” to it? Alternatively, yeah, post this (possibly on the Rails  
PH\> list) with more relatively info.

Ok … this is for the next person who has this error and is/was as  
confused as I was.

Rails is pointing to the wrong place. What Paul and the other posters  
were tell me was that the problem lies deeper in the code.

I was confused because I thought that the error was in my \*.erb file.  
Instead … as all the posters insisted … it was in the method being  
called and not in the caller.

* * *

Thank you all for your help and patience.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 15, 2010, 10:17am UTC](https://www.ruby-forum.com/t/cant-convert-symbol-into-string/183459/7 "2010-02-15T10:17:41Z")

</div>

Ralph S. wrote:

> I posted the question here because I am asking a Ruby question.
> 
> Let me rephrase the question …
> 
> Under what circumstances will Ruby not be able to convert a symbol into  
> a string?

In general, Ruby will never convert a Symbol into a String.

However:

- There are some built-in methods which accept either a Symbol or a  
String. For example,

send(:foo)  
send(“foo”)

work the same, because send will accept a String and convert it to a  
Symbol automatically.

- Similarly, you may come across some third-party libraries which will  
accept either a Symbol or a String, and convert automatically if  
required (perhaps by calling #to\_s or #to\_sym on the argument). Rails’  
HashWithIndifferentAccess is an example.

- In Ruby 1.9, Symbol does duck-type more closely to a String.

# 1.9.2

> > :bar[1]  
> > =\> “a”

# 1.8.6

> > :bar[1]  
> > NoMethodError: undefined method `[]’ for :bar:Symbol  
> > from (irb):1

But in general, it’s up to you to look at either the documentation or  
the source code of the thing that you’re calling, to see if it requires  
you to pass a String or a Symbol, or will accept either.

HTH,

Brian.
