I'm relatively new to Rails. I'm trying to create an application that can allow users to create video game items and store them under their own users. I'm using the latest version of Rails and Devise.
Using scaffolding as a base, I created the Videogame model/controller within my application. After linking the video game models to the user who created them, it seems that any attributes that are entered into the creation form are not saving, or at the very least just not showing up on the videogames/index page. After trying to search around on Google and StackOverflow, I couldn't find any similar questions/guides to work with.
Any ideas on how to fix this? Any help for a Rails newbie would be greatly appreciated.
Below I've posted all files that may be relevant. Please let me know if anything else is needed. To see the whole project, see http://github.com/bmmart2/collection-manager
Index page of two created items
Here is my controller:
class VideogamesController < ApplicationController
before_action :set_videogame, only: [:show, :edit, :update, :destroy]
# GET /videogames
# GET /videogames.json
def index
if user_signed_in?
@videogame = current_user.videogames.all
else
redirect_to :root
end
end
# GET /videogames/1
# GET /videogames/1.json
def show
end
# GET /videogames/new
def new
@videogame = current_user.videogames.new
end
# GET /videogames/1/edit
def edit
end
# POST /videogames
# POST /videogames.json
def create
@videogame = current_user.videogames.create(videogame_params)
respond_to do |format|
if @videogame.save
format.html { redirect_to @videogame, notice: 'Videogame was successfully created.' }
format.json { render :show, status: :created, location: @videogame }
else
format.html { render :new }
format.json { render json: @videogame.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /videogames/1
# PATCH/PUT /videogames/1.json
def update
respond_to do |format|
if @videogame.update(videogame_params)
format.html { redirect_to @videogame, notice: 'Videogame was successfully updated.' }
format.json { render :show, status: :ok, location: @videogame }
else
format.html { render :edit }
format.json { render json: @videogame.errors, status: :unprocessable_entity }
end
end
end
# DELETE /videogames/1
# DELETE /videogames/1.json
def destroy
@videogame.destroy
respond_to do |format|
format.html { redirect_to videogames_url, notice: 'Videogame was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_videogame
@videogame = Videogame.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def videogame_params
params.require(:videogame).permit(:title, :publisher, :platform, :year, :condition, :upc)
end
end
Videogame model:
class Videogame < ApplicationRecord
belongs_to :user
attr_accessor :title, :platform, :upc, :condition, :publisher, :year
end
Videogame db migration file:
class CreateVideogames < ActiveRecord::Migration[5.2]
def change
create_table :videogames do |t|
t.string :title
t.string :publisher
t.integer :condition
t.string :platform
t.string :year
t.string :upc
t.timestamps
end
add_index :videogames, :user_id
end
end
add_user_refs_to_videogame migration:
class AddUserRefsToVideogame < ActiveRecord::Migration[5.2]
def change
add_reference :videogames, :user, foreign_key: true
end
end
Edit: show view for video game
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @videogame.title %>
</p>
<p>
<strong>Publisher:</strong>
<%= @videogame.publisher %>
</p>
<p>
<strong>Platform:</strong>
<%= @videogame.platform %>
</p>
<p>
<strong>Year:</strong>
<%= @videogame.year %>
</p>
<p>
<strong>Condition:</strong>
<%= @videogame.condition %>
</p>
<p>
<strong>Upc:</strong>
<%= @videogame.upc %>
</p>
<%= link_to 'Edit', edit_videogame_path(@videogame) %> |
<%= link_to 'Back', videogames_path %>
I believe the attr_accessor
line in your videogame.rb
file is causing the problem. Try deleting it and see if that fixes the problem.