Reputation: 73
First time poster, long time lurker here. I have a Users model and controller for a little video game application for Rails that I'm currently making. So I've read a couple of answers on here regarding this issue, but none of the answers really seem to have helped me. People have suggested adding a "user_id" column to my Users table, but my point of contention is, I thought the "user_id" was automatically made in Rails? Even if I use a user.inspect
, I still see a user_id=7
show up on the page. However, I still get the unknown attribute error when attempting to create a game and assign to the current user. Any help would be most appreciated in pinpointing the cause and solution to this. Thanks!
app/controllers/users_controller.rb:
class UsersController < ApplicationController
skip_before_filter :require_authentication, only: [:new, :create]
def index
@users = User.all
end
def show
end
def new
@user = User.new
end
def edit
@user = current_user
end
def create
@user = User.create!(user_params)
session[:user_id] = @user.id
redirect_to users_path, notice: "Hi #{@user.username}! Welcome to DuckGoose!"
end
def update
current_user.update_attributes!(user_params)
redirect_to users_path, notice: "Successfully updated profile."
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
end
private
def user_params
params.require(:user).permit(:username, :firstname, :lastname, :email, :password, :password_confirmation)
end
end
app/config/routes.rb:
NkuProject::Application.routes.draw do
resources :users do
resources :games
end
resources :sessions
resources :games
get "sign_out", to: "sessions#destroy"
get "profile", to: "users#edit"
root to: "sessions#new"
end
app/controllers/games_controller.rb
class GamesController < ApplicationController
def new
@game = Game.new
end
def index
@games = Game.all
end
def destroy
@game = Game.find(params[:id])
@game.destroy
redirect_to games_url, notice: 'Game was successfully deleted.'
end
def create
@game = current_user.games.build(game_params)
if @game.save
redirect_to @game, notice: "Game successfully added"
else
render :new
end
end
def show
@game = Game.find(params[:id])
end
private
def game_params
params.require(:game).permit!
end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :require_authentication
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id].present?
end
helper_method :current_user
def require_authentication
if current_user
true
else
redirect_to new_session_path
end
end
end
I'm sure I'm missing some code to put in for reference, but if I need anything else please let me know.
Upvotes: 0
Views: 1022
Reputation: 53048
Looking at the way your controller actions are defined, I can safely say that User
and Game
have a 1-M relationship
, i.e.,
class User < ActiveRecord::Base
has_many :games
end
class Game < ActiveRecord::Base
belongs_to :user
end
Now, based on that games
table must have a field named user_id
. Rails is not going to create it for you unless you specify it. You need to add field user_id
in games
table by creating a migration for the same. Right now, it doesn't seem like you have user_id
foreign_key field in games
table. Hence, the error while saving games
record.
Upvotes: 2