Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No explicit conversion of Symbol into String" for new records in rails 4.0.1 (only)

After my rails 4 upgrade, trying to create a new record for any of my ActiveRecord classes gives

No explicit conversion of Symbol into String

For example, here is my links links_params method

def link_params
  params.require(:link)
  .permit(:url_address, :group_id, :alt_text, :version_number, 
  :position, :content_date, :verified_date) # This is line 157
end

# line 118 is: @link = Link.new(link_params)

but I get

TypeError (no implicit conversion of Symbol into String):
  app/controllers/links_controller.rb:157:in `link_params'
  app/controllers/links_controller.rb:118:in `create'

This error occurred while loading the following files:
link

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=",
 "link"=>{"url_address"=>"http://www.google.com",
 "alt_text"=>"",
 "version_number"=>"",
 "group_id"=>"49",
 "content_date"=>"08/18/2014"},
 "commit"=>"Save"}
like image 272
Michael Durrant Avatar asked Aug 19 '14 01:08

Michael Durrant


1 Answers

I'm not sure how it happened, but it looks like your params object is just a hash... and not an ActionController::Parameters object. When params is just a hash:

params = {"utf8"=>"✓", "authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=", "link"=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"}, "commit"=>"Save"}

params.require(:link)
=> TypeError: no implicit conversion of Symbol into String

params.class
=> Hash

But if it's an ActionController::Parameters object

params2 = ActionController::Parameters.new({"utf8"=>"✓", "authenticity_token"=>"0FqFTx2EjCIO+R+rm97lF15+id4b452n+dBuUNxAL9U=", "link"=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"}, "commit"=>"Save"})

params2.require(:link)
=>{"url_address"=>"http://www.google.com", "alt_text"=>"", "version_number"=>"", "group_id"=>"49", "content_date"=>"08/18/2014"} 

params2.class
=>ActionController::Parameters

Are you doing something to params before link_params gets a hold of it?

EDIT: According to the API Docs, Strong params is only available in Rails 4.0.2. If you're using an earlier version, you will have to stick with Rails 3 attr_accessible

like image 108
JTG Avatar answered Sep 19 '22 13:09

JTG