Ruby on rails 使用脚手架创建项目,创建后不显示属性-Rails/Desive
我对Rails比较陌生。我正在尝试创建一个应用程序,允许用户创建视频游戏项目并将其存储在自己的用户下。我正在使用Rails和Desive的最新版本 使用脚手架作为基础,我在应用程序中创建了视频游戏模型/控制器。将视频游戏模型链接到创建它们的用户之后,似乎任何输入到创建表单中的属性都没有保存,或者至少没有显示在视频游戏/索引页面上。在尝试在Google和StackOverflow上搜索之后,我找不到任何类似的问题/指南 有没有办法解决这个问题?任何对Rails新手的帮助都将不胜感激 下面我已经发布了所有可能相关的文件。如果还需要什么,请告诉我。要查看整个项目,请参见 这是我的控制器:Ruby on rails 使用脚手架创建项目,创建后不显示属性-Rails/Desive,ruby-on-rails,devise,scaffolding,Ruby On Rails,Devise,Scaffolding,我对Rails比较陌生。我正在尝试创建一个应用程序,允许用户创建视频游戏项目并将其存储在自己的用户下。我正在使用Rails和Desive的最新版本 使用脚手架作为基础,我在应用程序中创建了视频游戏模型/控制器。将视频游戏模型链接到创建它们的用户之后,似乎任何输入到创建表单中的属性都没有保存,或者至少没有显示在视频游戏/索引页面上。在尝试在Google和StackOverflow上搜索之后,我找不到任何类似的问题/指南 有没有办法解决这个问题?任何对Rails新手的帮助都将不胜感激 下面我已经发布
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视频游戏控制器
电子游戏模式:
class Videogame < ApplicationRecord
belongs_to :user
attr_accessor :title, :platform, :upc, :condition, :publisher, :year
end
class视频游戏
视频游戏数据库迁移文件:
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
class CreateVideogames
将用户参考添加到视频游戏迁移:
class AddUserRefsToVideogame < ActiveRecord::Migration[5.2]
def change
add_reference :videogames, :user, foreign_key: true
end
end
class AddUserRefsToVideogame
编辑:显示视频游戏的视图
<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 %>
标题:
出版商:
平台:
年份:
条件:
Upc:
|
我相信是您的videogame.rb
文件中的attr\u accessor
行造成了问题。尝试删除它,看看是否可以解决问题。您的“显示视图”是什么样子的?我已经为您添加了它。您是否使用“设计生成器”创建了用户?在控制器中,将@videogame
更改为@videogames
。这可能对索引视图有所帮助。您是否在您的用户模型中添加了许多:视频游戏?是的,我相信我们生成了所有必要的文件,例如各种用户控制器和设计视图。我们的身份验证似乎工作正常,因为当我们在测试用户下创建视频游戏条目时,它们只显示该用户创建的条目,而不显示其他用户创建的条目。如果你有兴趣看整个项目,我有它在谢谢你!这完美地解决了问题。我有一种感觉,这将是一个简单的修复。您知道为什么attr_访问器会导致这种情况吗?