创建带脚手架的项目不会在创建后显示属性 - 导轨/设计



我对导轨相对较新。我正在尝试创建一个应用程序,该应用程序可以允许用户创建视频游戏并将其存储在自己的用户下。我正在使用最新版本的Rails并设计。

使用脚手架作为基础,我在应用程序中创建了视频游戏模型/控制器。将视频游戏模型链接到创建它们的用户之后,似乎输入到创建表单中的任何属性都无法保存,或者至少只是在视频游戏/索引页面上没有显示。尝试在Google和Stackoverflow上进行搜索之后,我找不到任何类似的问题/指南。

关于如何解决此问题的任何想法?对Rails Newbie的任何帮助将不胜感激。

下面我发布了所有可能相关的文件。请让我知道是否需要其他任何东西。要查看整个项目,请参见http://github.com/bmmart2/collection-manager

项目创建后的图像

两个创建项目的索引页

这是我的控制器:

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

电子游戏DB迁移文件:

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行正在引起问题。尝试删除它,看看是否解决了问题。

最新更新