Owen Carter Owen Carter - 1 year ago 51
MySQL Question

ActiveRecord::StatementInvalid in UsersController#create (Insert does not include newly added column)

I'm presently teaching myself ruby on rails following my degree. I'm really new to the language and want to know more about how migrations work.

Error Of problem

I have updated my existing users table through migration to allow for an additional column 'username' and included some HTML/Ruby to allow the user to enter in the field. I can't fathom how rails generates INSERT SQL statements or updates them upon migration.

Below is my Code for HTML

<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<% end %>

<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>

<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>

<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>

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

This is the schema after migration

Schema After rake db:migrate

I must be missing something...

Answer Source

If you look at the server logs, you should find something like Unpermitted parameter username.

You should permit the username field in the list of whitelisted parameters. I assume you have a user_params method in your users_controller.rb. Modify it to allow the newly created username field.

def user_params
  params.require(:user).permit(:username, :password, :email)