2012-10-26 95 views
0

直接在生成腳手架後,當嘗試創建一些新的測試虛擬數據時,我得到一個NoMethodError。CharactersController中的Ruby on Rails NoMethodError#new

我是新來的鐵軌,所以我可能做了一些簡單的錯誤,但它似乎並不像鐵軌應該產生錯誤的權利,從開始吧。

型號:

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 
+0

你可以添加你得到過具體的錯誤? –

回答

4

您成功地使用一些保留字作爲你的屬性。具體而言,class不應被用作模型屬性。你會注意到character.class在你的視圖中被調用,它應該返回'Character',該特定角色對象的類名。

這裏的保留關鍵字的列表:http://oldwiki.rubyonrails.org/rails/pages/ReservedWords

+0

謝謝!這似乎是它。我真的找不到解決我所做過的事情的方法,但是在項目的早期階段我就可以重新開始,然後再調用其他類。 – stygma

相關問題