-1

我有我使用以下語法創建一個viewcontrollermodelmigration一個簡單的項目:而在模型協會

rails g scaffold user name:string contact:string 

然後我創建了一個模型只用下面的命令:

rails g model event event_name:string event_place:string 

然後我在模型中創建關聯,您可以在下面的代碼中看到。

代碼的 user.rb

是以下:

代碼的 event.rb
class User < ActiveRecord::Base 
    has_many :events 


    attr_accessible :contact, :name, :events_attributes 

    accepted_nested_attributes_for :events 

end 

類以下:

class Event < ActiveRecord::Base 
    belongs_to :user 

    attr_accessible :event_name, :event_place 
end 

index.html.erb的代碼如下:

<h1>Listing users</h1> 

<table> 
    <tr> 
    <th>Name</th> 
    <th>Contact</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @users.each do |user| %> 
    <tr> 
    <td><%= user.name %></td> 
    <td><%= user.contact %></td> 
    <td><%= link_to 'Show', user %></td> 
    <td><%= link_to 'Edit', edit_user_path(user) %></td> 
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New User', new_user_path %> 

和我還沒有在我看來,沒有改變任何東西.....作爲scaffo ld生成自己。當我在瀏覽器上運行我的代碼,它顯示了以下錯誤:

NoMethodError in Users#index 

Showing /home/wasif/projects/club/app/views/users/index.html.erb where line #12 raised: 

undefined method `each' for nil:NilClass 

Extracted source (around line #12): 

9:  <th></th> 
10: </tr> 
11: 
12: <% @users.each do |user| %> 
13: <tr> 
14:  <td><%= user.name %></td> 
15:  <td><%= user.contact %></td> 

這是users_controller.rb

class UsersController < ApplicationController 
    # GET /users 
    # GET /users.json 
    def index 
    @users = user.all 

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

    # GET /users/1 
    # GET /users/1.json 
    def show 
    @User = User.find(params[:id]) 

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

    # GET /user/new 
    # GET /user/new.json 
    def new 
    @user = User.new 

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

    # GET /user/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    end 

    # POST /users 
    # POST /users.json 
    def create 
    @user = User.new(params[:user]) 

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

    # PUT /users/1 
    # PUT /users/1.json 
    def update 
    @user = User.find(params[:id]) 

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

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 

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

我不知道現在該做什麼,我怎麼能刪除錯誤,作爲即時通訊初學者代碼所以請幫我現在做什麼。提前致謝!

+1

請顯示您的'users_controller.rb'。 –

+0

你在你的控制器代碼中缺少一些東西。在'UsersController'代碼這裏。 –

+0

你運行過'rake db:migrate'嗎? –

回答

1

U的索引方法錯誤代碼:

@users = User.all應改爲使用的@users = user.all

def index 
    @users = User.all 

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

我也在想。 user.all應該給你一個錯誤。嗯。 –

+0

user.all是我的打字錯誤...它的代碼中完美無瑕的User.all ...但有相同的錯誤。 –

-1

請檢查數據庫,並確保你在表列「user_ID的」「活動」

+0

這不會導致這裏描述的錯誤。 – Mischa