0

直接在生成脚手架后,我在尝试创建一些新的测试虚拟数据时收到 NoMethodError。

我是 Rails 的新手,所以我可能做错了一些简单的事情,但 Rails 似乎不应该从一开始就生成带有错误的文件。

模型:

class Character < ActiveRecord::Base
  attr_accessible :cha, :class, :class_option, :con, :dex, :exp, :int, :name, :portrait_url, :str, :wis
end

看法:

<h1>Listing characters</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Exp</th>
    <th>Class</th>
    <th>Class option</th>
    <th>Str</th>
    <th>Dex</th>
    <th>Con</th>
    <th>Int</th>
    <th>Wis</th>
    <th>Cha</th>
    <th>Portrait url</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @characters.each do |character| %>
  <tr>
    <td><%= character.name %></td>
    <td><%= character.exp %></td>
    <td><%= character.class %></td>
    <td><%= character.class_option %></td>
    <td><%= character.str %></td>
    <td><%= character.dex %></td>
    <td><%= character.con %></td>
    <td><%= character.int %></td>
    <td><%= character.wis %></td>
    <td><%= character.cha %></td>
    <td><%= character.portrait_url %></td>
    <td><%= link_to 'Show', character %></td>
    <td><%= link_to 'Edit', edit_character_path(character) %></td>
    <td><%= link_to 'Destroy', character, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

控制器:

class CharactersController < ApplicationController
  # GET /characters
  # GET /characters.json
  def index
    @characters = Character.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @characters }
    end
  end

  # GET /characters/1
  # GET /characters/1.json
  def show
    @character = Character.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @character }
    end
  end

  # GET /characters/new
  # GET /characters/new.json
  def new
    @character = Character.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @character }
    end
  end

  # GET /characters/1/edit
  def edit
    @character = Character.find(params[:id])
  end

  # POST /characters
  # POST /characters.json
  def create
    @character = Character.new(params[:character])

    respond_to do |format|
      if @character.save
        format.html { redirect_to @character, notice: 'Character was successfully created.' }
        format.json { render json: @character, status: :created, location: @character }
      else
        format.html { render action: "new" }
        format.json { render json: @character.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /characters/1
  # PUT /characters/1.json
  def update
    @character = Character.find(params[:id])

    respond_to do |format|
      if @character.update_attributes(params[:character])
        format.html { redirect_to @character, notice: 'Character was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @character.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /characters/1
  # DELETE /characters/1.json
  def destroy
    @character = Character.find(params[:id])
    @character.destroy

    respond_to do |format|
      format.html { redirect_to characters_url }
      format.json { head :no_content }
    end
  end
end
4

1 回答 1

4

您设法使用一些保留字作为您的属性。特别class不应该用作模型属性。您会注意到character.class在您的视图中被调用,它应该返回“Character”,即该特定角色对象的类名。

这是保留关键字的列表:http: //oldwiki.rubyonrails.org/rails/pages/ReservedWords

于 2012-10-26T04:38:11.550 回答