创建带有脚手架的项目在创建后不显示属性 - Rails/Devise
Creating items with scaffolding not showing attributes after creation - Rails/Devise
我对 Rails 比较陌生。我正在尝试创建一个应用程序,允许用户创建视频游戏项目并将它们存储在他们自己的用户名下。我正在使用最新版本的 Rails 和 Devise。
使用脚手架作为基础,我在我的应用程序中创建了视频游戏 model/controller。将视频游戏模型链接到创建它们的用户后,似乎在创建表单中输入的任何属性都没有保存,或者至少没有显示在 videogames/index 页面上。在尝试搜索 Google 和 Whosebug 后,我找不到任何类似的 questions/guides 可以使用。
关于如何解决这个问题有什么想法吗?对 Rails 新手的任何帮助将不胜感激。
下面我发布了所有可能相关的文件。如果还需要什么,请告诉我。要查看整个项目,请参阅 http://github.com/bmmart2/collection-manager
Image after item creation
Index page of two created items
这是我的控制器:
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
电子游戏模型:
class Videogame < ApplicationRecord
belongs_to :user
attr_accessor :title, :platform, :upc, :condition, :publisher, :year
end
视频游戏数据库迁移文件:
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 迁移:
class AddUserRefsToVideogame < ActiveRecord::Migration[5.2]
def change
add_reference :videogames, :user, foreign_key: true
end
end
编辑:显示视频游戏视图
<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 %>
我认为您的 videogame.rb
文件中的 attr_accessor
行导致了问题。尝试删除它,看看是否能解决问题。
我对 Rails 比较陌生。我正在尝试创建一个应用程序,允许用户创建视频游戏项目并将它们存储在他们自己的用户名下。我正在使用最新版本的 Rails 和 Devise。
使用脚手架作为基础,我在我的应用程序中创建了视频游戏 model/controller。将视频游戏模型链接到创建它们的用户后,似乎在创建表单中输入的任何属性都没有保存,或者至少没有显示在 videogames/index 页面上。在尝试搜索 Google 和 Whosebug 后,我找不到任何类似的 questions/guides 可以使用。
关于如何解决这个问题有什么想法吗?对 Rails 新手的任何帮助将不胜感激。
下面我发布了所有可能相关的文件。如果还需要什么,请告诉我。要查看整个项目,请参阅 http://github.com/bmmart2/collection-manager
Image after item creation
Index page of two created items
这是我的控制器:
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
电子游戏模型:
class Videogame < ApplicationRecord
belongs_to :user
attr_accessor :title, :platform, :upc, :condition, :publisher, :year
end
视频游戏数据库迁移文件:
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 迁移:
class AddUserRefsToVideogame < ActiveRecord::Migration[5.2]
def change
add_reference :videogames, :user, foreign_key: true
end
end
编辑:显示视频游戏视图
<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 %>
我认为您的 videogame.rb
文件中的 attr_accessor
行导致了问题。尝试删除它,看看是否能解决问题。