can't convert Symbol into String

R

Ralph Shnelvar

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. (e-mail address removed))</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?
 
B

Brian Candler

Ralph said:
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.
 
R

Ralph Shnelvar

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


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.

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
 
A

Aurora S.r.l.

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

...

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
 
P

Paul Harrington

Ralph said:
Sunday, February 14, 2010, 2:46:02 PM, you 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?

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.
 
R

Ralph Shnelvar

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


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.
 
B

Brian Candler

Ralph said:
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
=> "a"

# 1.8.6
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top