Jen Lijó Jen Lijó - 3 months ago 9
Ruby Question

NoMethodError in Users#index || database issue

I'm new to RoR. I've been following a tutorial where I had to generate scaffold user first_name last_name and then migrate to the DB. For some reason, when I tried to push to a new branch in Git, some of the changes were lost. Then I couldn't load the local server, getting an error related to the DB. After long hours trying to figure out what was wrong I gave up and decided to destroy and re-migrate the DB.

I've tried to generate scaffold user first_name last_name, but console gives me an error:

The name 'User' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.


Whilst the index page looks ok and I can create users and log in/log out normally, when I try to access http://localhost:3000/users/, I get this error:

NoMethodError in Users#index
Showing /Users/Jen/nameofapp/app/views/users/index.html.erb where line #16 raised:

undefined method `first_name' for # User:0x007febdf5938a0


These are my code snippets:

views/users/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.first_name %></td>
<td><%= user.last_name %></td>
<td><%= link_to 'Show', user , class:"btn btn-default btn-xs" %></td>
<td><%= link_to ('<span class="glyphicon glyphicon-pencil"></span>').html_safe, edit_user_path(user) %></td>
<td><%= link_to ('<span class="glyphicon glyphicon-remove"></span>').html_safe, user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>

<div class="col-sm-6 col-md-4">
<%= link_to 'New User', new_user_path, class:"btn btn-default btn-xs" %>
</div>


models/user.rb

class User < ApplicationRecord

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many :orders
end


controllers/users_controller.rb

class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
before_action :set_user, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource

# GET /users
# GET /users.json
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users
# POST /users.json
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

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end

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)
end
end

Answer

You can use scaffold if you have already used Devise for User class. However, you can create another migration file to add these columns to users table

rails g migration AddFieldsToUser

Then inside the migration

def change
  add_column :users, :first_name, :string
  add_column :users, :last_name, :string
end

As you are a newbie, im writing these code snippets.