Note that there are some explanatory texts on larger screens.

plurals
  1. POhaving error while association in models
    primarykey
    data
    text
    <p>i have a simple project in which i created a <code>view</code>, <code>controller</code>,<code>model</code>, <code>migration</code> using the following syntax: </p> <pre><code>rails g scaffold user name:string contact:string </code></pre> <p>then i created a model only with the following command:</p> <pre><code>rails g model event event_name:string event_place:string </code></pre> <p>then i created association in the models which you can see in the following code.</p> <p>code of <code>user.rb</code> class is following:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :events attr_accessible :contact, :name, :events_attributes accepted_nested_attributes_for :events end </code></pre> <p>code of <code>event.rb</code> class is following:</p> <pre><code>class Event &lt; ActiveRecord::Base belongs_to :user attr_accessible :event_name, :event_place end </code></pre> <p>the code of the <code>index.html.erb</code> is following:</p> <pre><code>&lt;h1&gt;Listing users&lt;/h1&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Contact&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;% @users.each do |user| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= user.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= user.contact %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Show', user %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_user_path(user) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; &lt;br /&gt; &lt;%= link_to 'New User', new_user_path %&gt; </code></pre> <p>and i haven't changed anything in my views..... as the scaffold generate itself. when i run my code on browser it shows following error:</p> <pre><code> 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: &lt;th&gt;&lt;/th&gt; 10: &lt;/tr&gt; 11: 12: &lt;% @users.each do |user| %&gt; 13: &lt;tr&gt; 14: &lt;td&gt;&lt;%= user.name %&gt;&lt;/td&gt; 15: &lt;td&gt;&lt;%= user.contact %&gt;&lt;/td&gt; </code></pre> <p>This is the code of <code>users_controller.rb</code></p> <pre><code>class UsersController &lt; 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 </code></pre> <p>I dont know what to do now, and how can i remove error, as i m beginner so please help me what to do now. Thanks in Advance! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload