0

So I've been trying to use validations on ruby on rails with the form_for format

I followed this: https://stackoverflow.com/a/33048292/8054234

but im getting the following error:

<%= pluralize(object.errors.count, "error") %> prevented the form from being saved:

undefined method `errors' for :user:Symbol

Any idead how I could make this work/fix it?

edit1 uploaded full form code:

<%= stylesheet_link_tag "users" %>

<div class="jumbotron"
    <div class="container">

    <h2>Signup</h2>


<%= form_for :user, url: '/users' do |f| %>
  <%= form_errors_for :user %>
  Número de Empregado: <br>
  <%= f.number_field :NumeroEmpregado %><br>
  Primeiro e Último Nome: <br>
  <%= f.text_field :nome %><br>
  Password: <br>
  <%= f.password_field :password %><br>
  Confirmação Password: <br>
  <%= f.password_field :password_confirmation %><br>


  <%= f.submit "Submit" %>

    </div>
</div>

<% end %>

2 Answers2

2

object must be an instance of an ActiveRecord model. You are passing :user as a symbol.

You should pass an instance of User. If you use some authentication logic you could pass current_user to this method.

UPD:

The error says that your object does not understand method errors. :user is a Symbol. It is not a variable that holds your user. Therefore, it does not implement method errors.

User is a model that inherits behaviour from ActiveRecord. It provides method errors. For example, object = User.create(params) will work.

yzalavin
  • 1,788
  • 1
  • 13
  • 19
0

EDITED

In your UsersController in new action update as below

class UsersController < ApplicationController
  def new
    @user = User.new
  end
end

and in your views/users/_form.html.erb file form do the below code

<%= 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 %>

  Número de Empregado: <br>
  <%= f.number_field :NumeroEmpregado %><br>

  Primeiro e Último Nome: <br>
  <%= f.text_field :nome %><br>

  Password: <br>
  <%= f.password_field :password %><br>

 Confirmação Password: <br>
 <%= f.password_field :password_confirmation %><br>

 <%= f.submit "Submit" %>

<% end %>
hgsongra
  • 1,454
  • 15
  • 25