-1

我有一个简单的项目,我在其中创建了一个view, controller, model,migration使用以下语法:

rails g scaffold user name:string contact:string

然后我只使用以下命令创建了一个模型:

rails g model event event_name:string event_place:string

然后我在模型中创建了关联,您可以在下面的代码中看到。

类代码user.rb如下:

class User < ActiveRecord::Base
    has_many :events


  attr_accessible :contact, :name, :events_attributes

  accepted_nested_attributes_for :events

end

类代码event.rb如下:

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 %>

而且我的观点没有任何改变.....因为脚手架会自行生成。当我在浏览器上运行我的代码时,它显示以下错误:

 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

我不知道现在该做什么,以及如何消除错误,因为我是初学者,所以请帮助我现在该做什么。提前致谢!

4

2 回答 2

1

U have wrong code in index method:

@users = User.all should be used instead of @users = user.all

  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end
于 2013-08-19T09:21:54.883 回答
-1

请检查数据库并确保您在表“事件”中有列“user_id”

于 2013-08-19T12:39:18.453 回答