2

I get the error no implicit conversion of Symbol into String when I try to create a new record, which I don't understand because all data is formed of strings.

In the controller I have:

def new
    @account = Account.new
end

def create
    @account = Account.new(account_params)
    if @account.save
      redirect_to(:action => 'index')
    else
      render('new')
    end
end

private
    def account_params
    params.require(:account).permit(:username, :firstname, :lastname, :organisation, :phonenumber, :avatar, :about, :verified)
end

And the form is simply:

<%= form_for(@account) do |f| %>
  <div>
   <%= f.label :username %><br>
   <%= f.text_field :username %>
  </div>
  <div>
   <%= f.label :firstname %><br>
   <%= f.text_field :firstname %>
  </div>
  <div>
   <%= f.label :lastname %><br>
   <%= f.text_field :lastname %>
  </div>
  ... more of the same...

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And the migration file:

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|

      t.string   :username
      t.string   :firstname
      t.string   :lastname
      t.string   :organisation
      ...more of the same, all strings...
    end
  add_index :accounts, :username,                unique: true
  end
end

I'm using Rails 3.2 and Ruby 2.1.5.

Puce
  • 1,003
  • 14
  • 28
Nick
  • 3,496
  • 7
  • 42
  • 96
  • What is your question? – sawa Feb 01 '15 at 19:48
  • Sorry, I seem to have been a bit unclear. First, I mistakenly thought I was using Ruby 4 but ```ruby -v``` tells me that it's version 2.1.5. that I'm using. Sorry about that. Second, my question is what am I doing wrong? What is causing the error ```no implicit conversion of Symbol into String``` when I try to create a new record via http://localhost:3000/accounts/new/? – Nick Feb 01 '15 at 19:53
  • probably this issue is related: http://stackoverflow.com/questions/25374559/no-explicit-conversion-of-symbol-into-string-for-new-records-in-rails-4 – Rustam Gasanov Feb 01 '15 at 20:10
  • I've read the different posts on this error message but unfortunately without a solution. By the way, the full error message is: ```TypeError in AccountsController#create ``` ```no implicit conversion of Symbol into String ``` ```app/controllers/accounts_controller.rb:59:in `account_params' ``` ```app/controllers/accounts_controller.rb:17:in `create' ``` ```This error occurred while loading the following files: account``` – Nick Feb 01 '15 at 20:39
  • Also updating to rails 3.2.21 has not worked (no difference). Any suggestions what else I can do? – Nick Feb 01 '15 at 21:28

2 Answers2

3

I found the solution: In the controller I had to change

@account = Account.new(account_params)

to

@account = Account.new(params[:account])
Nick
  • 3,496
  • 7
  • 42
  • 96
2

Rails 3.2 doesn't include strong parameters. If you want to use them, you need to add the gem 'strong_parameters' to your Gemfile.

This stack overflow answer talks about the necessary steps.

Strong Parameters in Rails 3.2.8

Community
  • 1
  • 1
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53