Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can't convert Symbol into String

85 views
Skip to first unread message

Ralph Shnelvar

unread,
Feb 14, 2010, 4:05:22 PM2/14/10
to
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: <div id="main">
18: <p><%= shnView.label('nickname') %> <i>(e.g. Craig10027)</i><br>
19: <%= f.text_field :nickname %></p>
20: <p><%= shnView.label(:email) %> <i>(e.g. Craig10027)</i> <i>(e.g. Cr...@NoUniverse.tv)</i><br>
21: <%= f.text_field :email %></p>
22: <p><%= f.label :password %><br>
23: <%= f.password_field :password %></p>

- - - - -

Why can't my shnView.label(:email) method create a symbol to sting conversion ... but f.text_field can?


Brian Candler

unread,
Feb 14, 2010, 4:46:02 PM2/14/10
to
Ralph Shnelvar 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.

--
Posted via http://www.ruby-forum.com/.

Ralph Shnelvar

unread,
Feb 14, 2010, 4:54:50 PM2/14/10
to
Sunday, February 14, 2010, 2:46:02 PM, you wrote:

BC> Ralph Shnelvar 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

Aurora S.r.l.

unread,
Feb 14, 2010, 5:39:43 PM2/14/10
to
Ralph Shnelvar 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

Paul Harrington

unread,
Feb 14, 2010, 6:26:32 PM2/14/10
to
Ralph Shnelvar wrote:
> Sunday, February 14, 2010, 2:46:02 PM, you wrote:
>
> BC> Ralph Shnelvar 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.

Ralph Shnelvar

unread,
Feb 14, 2010, 10:48:42 PM2/14/10
to
Sunday, February 14, 2010, 4:26:32 PM, you wrote:

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

>> BC> Ralph Shnelvar 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.


Brian Candler

unread,
Feb 15, 2010, 4:21:37 AM2/15/10
to
Ralph Shnelvar 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,

0 new messages