can't convert Symbol into String
It seems like you are trying to use strong paramaters. You get this error cannot convert symbol into string because you have not configured the strong_parameters. So by default you cant use require on params with symbols.
Configure strong parameters as follows:
1.) Add gem 'strong_parameters' to your gemfile and bundle it.
2.) Include Restrictions to you model as follows.
include ActiveModel::ForbiddenAttributesProtection to your model.
3.) Disable white listing in application confiuration(config/application.rb)
config.active_record.whitelist_attributes = false
See the documentation for more details on configuring.
Now your code should work.
Why does implicit Symbol to String conversion' lead to TypeError in JavaScript?
Is the latter just calling
.toString()
on a newSymbol
and append (+
) it to empty string?
No actually, Symbols cannot be implicitly cast to strings, or numbers, although interestingly enough you can implicitly cast them to a boolean.
MDN actually has a section on some of these pitfalls:
Symbol type conversions
Some things to note when working with type conversion of symbols.
- When trying to convert a symbol to a number, a
TypeError
will be thrown (e.g.+sym
orsym | 0
).- When using loose equality,
Object(sym) == sym
returnstrue.
Symbol("foo") + "bar"
throws aTypeError
(can't convert symbol to string). This prevents you from silently creating a new string property name from a symbol, for example.- The "safer"
String(sym)
conversion works like a call toSymbol.prototype.toString()
with symbols, but note thatnew String(sym)
will throw.
This behavior is documented in the spec under the abstract ToString
operation:
Argument Type: Symbol
Result: Throw a
TypeError
exception.
And similarly for abstract ToNumber
operation:
Argument Type: Symbol
Result: Throw a
TypeError
exception.
To cast a Symbol
to a string without a TypeError
, you must use either the toString
method, or String()
.
Symbol to string issue
String interpolation is an implicit to_s
call. So, something like this:
result = "hello #{expr}"
is more or less equivalent to this:
result = "hello " + expr.to_s
As karim79 said, a symbol is not a string but symbols do have to_s
methods so your interpolation works; your attempt at using +
for concatenation doesn't work because there is no implementation of +
available that understand a string on the left side and a symbol on the right.
Why Symbols not convert string implicitly
According to ECMA-262, using the addition operator on a value of type Symbol in combination with a string value first calls the internal ToPrimitive, which returns the symbol. It then calls the internal ToString which, for Symbols, will throw a TypeError exception.
So calling the internal ToString is not the same as calling Symbol.prototype.toString.
So I guess the answer to:
Why does the implicit type conversion not work?
is "because the spec says so".
Rails - can't convert Symbol into String on Production ONLY
I suggest you just use key.to_s
as a quick workaround.
The reason for your problem may be that some version of some component differs between your testing server and the production server.
If your tests pass, and your production environment crashes, that is a very bad situation.
You should compare the versions of ruby and all of the gems you are using. If you use 'bundler' then 'bundle list' gives a nice summary.
If you find out that all the versions are the same... Well, we will be looking for another reason.
Update
As it seems that the problem is caused not by the version differences, but by unexpected data in flash, which obviously in production environment may be different than in testing.
I suggest you change the myModelFlash
method a little.
def myModelFlash( key )
if m = /^_mymodel_(.*)/.match(key.to_s)
return m[1]
end
end
The flash may contain different keys, some of them may be Symbols or really anything, so you must be prepared to handle all of them.
Converting the key
parameter with .to_s
should be a safe choice, but if you are sure that you always set the flash keys (I mean the keys related to this "_mymodel" issue) as Strings, you may change the first line of this method:
def myModelFlash( key )
if key.is_a?(String) && m = /^_mymodel_(.*)/.match(key.to_s)
return m[1]
end
end
And in your test, add a few other keys to your flash, and then test how the action handles them.
can't convert symbol into string ruby on rails
This is typo error in view
You have used
def new
@supplier=Supplier.new
end
And in view you have used
<%= form_for :suppliers, :url => {:action => "create"} do |f| %>
in the view :suppliers
must be supplier
delete s
from suppliers
Please use
def new
@supplier=Supplier.new
end
<%= form_for :supplier, :url => {:action => "create"} do |f| %>
<div class="field">
<%= f.text_area :supplier, placeholder: "Add new Supplier..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
date_field - can't convert Symbol into String
maybe a little late, but I found something here:
http://i18n.lighthouseapp.com/projects/14947/tickets/12-i18n-and-date_select-exception
Best regards,
Dinah
edit: I had to change date order in locale .yml to
- order: [ :year, :month, :day ]
+ order:
+ - :year
+ - :month
+ - :day
Related Topics
How to Organize Minitest/Unit Tests
How to Include a Yaml File Inside a Yaml File in Ruby
What Is the Best Wysiwyg for Rails - Ruby on Rails Blog
Using the Value of a Variable as Another Variables Name in Ruby
Stylesheet_Link_Tag :All Versus :Media =>All
How to Parse Consecutive Tags with Nokogiri
Rspec Testing Redirect to Url with Get Params
What's the Difference Between Ruby Core API and Standard Library API
How to Tokenize This String in Ruby
Ruby: How to Group a Ruby Array
Ruby on Rails: How to Check If a File Is an Image
Paperclip Amazon S3 Setup with Heroku
How to Extract the Sign of an Integer in Ruby
How to Remove Repeated Spaces in a String