Can't convert Symbol into String

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. [email protected])

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?

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.

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

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

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.

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.

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.