Frederick John Frederick John - 1 month ago 8
MySQL Question

Rails 5 'nomethod' error during sign up

I'm receiving the nomethod name error during user sign up. However, there isn't a name column in my users table in the database and there isn't a name field in the form so I don't understand why this error is being thrown. Here is the error and then my form, controller, and schema. noMethodError in Users#Create (wtf?)

<%= 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>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

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

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

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

<div class="field">
<%= f.label :display_name %>
<%= f.text_field :display_name %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


and here is my users controllers. The error is originating from the users controller create action.

def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end


And of course I thought it may have been the DB schema but it isn't. Here is the schema.

ActiveRecord::Schema.define(version: 20161109225217) do

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "display_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end

end


I have no idea what is causing this error. Any suggestions please!?

class User < ApplicationRecord
attr_accessor :password
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end


And my user params -

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :display_name, :password, :password_confirmation)
end
end

Answer

Go and check your model you made mistake in validate field

your code

 validates :name, presence: true, length: { maximum: 50 }`

change to like this

 validates :first_name, :last_name, presence: true, length: { maximum: 50 }