首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用脚手架创建项目,创建后不显示属性- Rails/Devise

使用脚手架创建项目,创建后不显示属性- Rails/Devise
EN

Stack Overflow用户
提问于 2019-02-16 02:34:39
回答 1查看 26关注 0票数 0

我对Rails比较陌生。我正在尝试创建一个应用程序,允许用户创建视频游戏项目并将其存储在自己的用户名下。我使用的是最新版本的Rails和Devise。

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

有什么办法解决这个问题吗?对Rails新手的任何帮助都将不胜感激。

下面我已经发布了所有可能相关的文件。如果还需要什么,请告诉我。要查看整个项目,请参阅http://github.com/bmmart2/collection-manager

Image after item creation

Index page of two created items

这是我的控制器:

代码语言:javascript
复制
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

视频游戏模型:

代码语言:javascript
复制
class Videogame < ApplicationRecord

belongs_to :user

attr_accessor :title, :platform, :upc, :condition, :publisher, :year

end

视频游戏数据库迁移文件:

代码语言:javascript
复制
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迁移:

代码语言:javascript
复制
class AddUserRefsToVideogame < ActiveRecord::Migration[5.2]
  def change
    add_reference :videogames, :user, foreign_key: true
  end
end

编辑:显示视频游戏视图

代码语言:javascript
复制
<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 %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-16 03:57:34

我认为videogame.rb文件中的attr_accessor行是导致问题的原因。尝试删除它,看看这是否解决了问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54715221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档