2012-05-23 101 views
1

我正在開發一個應用程序,用戶在其各自的頁面上顯示一組微博。我正在嘗試向這些微博添加評論。每次訪問本地主機:3000 /用戶/(user_id_#)Ruby on Rails:未定義的方法「評論」爲零:NilClass

我得到這個錯誤:未定義的方法`評論的零:NilClass

當用戶微柱顯示該錯誤只配備。否則,它只顯示他們的空白頁面。這個錯誤來自這個視圖應用/視圖/用戶/ show.html.erb這種觀點使這部分的,其中在13行

<li> 
    <span class="content"><%= micropost.content %></span> 
    <span class="timestamp"> 
    Posted <%= time_ago_in_words(micropost.created_at) %> ago. 
    </span> 
    <% if current_user?(micropost.user) %> 
    <%= link_to "delete", micropost, method: :delete, 
            confirm: "You sure?", 
            title: micropost.content %> 
    <% end %> 

    <h2>Comments</h2> 
    <% micropost.comments.each do |comment| %> 
    <p> 
     <b>Commenter:</b> 
     <%= comment.commenter %> 
    </p> 

    <p> 
     <b>Comment:</b> 
     <%= comment.body %> 
    </p> 
    <% end %> 

    <h3>Add a comment:</h3> 
    <%= form_for([micropost, micropost.comments.build]) do |f| %> 
    <div class="field"> 
     <%= f.label :commenter %><br /> 
     <%= f.text_field :commenter %> 
    </div> 
    <div class="field"> 
     <%= f.label :body %><br /> 
     <%= f.text_area :body %> 
    </div> 
    <div class="actions"> 
     <%= f.submit %> 
    </div> 
    <% end %> 
</li> 

這裏發生的錯誤是我comment.rb文件

class Comment < ActiveRecord::Base 
    belongs_to :micropost 
    attr_accessible :body, :user_id 

end 

和我micropost.rb文件

class Micropost < ActiveRecord::Base 
    attr_accessible :content 
    belongs_to :user 
    has_many :comments 

    validates :content, presence: true, length: { maximum: 140 } 
    validates :user_id, presence: true 
end 

和我comments_controller.rb

class CommentsController < ApplicationController 
    def create 
    @micropost = Micropost.find(params[:micropost_id]) 
    @comment = @micropost.comments.create(params[:comment]) 
    redirect_to micropost_path(@micropost) 
    end 

end 

最後我microposts_controller.rb

class MicropostsController < ApplicationController 
    before_filter :signed_in_user 
    before_filter :correct_user, only: :destroy 

    def create 
    @micropost = current_user.microposts.build(params[:micropost]) 
    if @micropost.save 
     flash[:success] = "Micropost created!" 
     redirect_to root_path 
    else 
     @feed_items = [] 
     render 'static_pages/home' 
    end 
    end 


    def new 
    @micropost = Micropost.new(params[:micropost]) 
    end 

    def show 

    @micropost = Micropost.find(params[:id]) 


    end 

    def destroy 
    @micropost.destroy 
    redirect_back_or root_path 
    end 

    private 

    def correct_user 
     @micropost = current_user.microposts.find_by_id(params[:id]) 
     redirect_to root_path if @micropost.nil? 
    end 
end 


    class CommentsController < ApplicationController 
     def create 
     @micropost = Micropost.find(params[:micropost_id]) 
     @comment = @micropost.comments.create(params[:comment]) 
     redirect_to micropost_path(@micropost) 
     end 

    end 

也在這裏是users_controller.rb

class UsersController < ApplicationController 
    before_filter :signed_in_user, 
       only: [:index, :edit, :update, :destroy, :following, :followers] 
    before_filter :correct_user, only: [:edit, :update] 
    before_filter :admin_user,  only: :destroy 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    def show 
    @user = User.find(params[:id]) 
    @micropost = @user.microposts.first 
    @comment = Comment.new 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User destroyed." 
    redirect_to users_path 
    end 

    def following 
    @title = "Following" 
    @user = User.find(params[:id]) 
    @users = @user.followed_users.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    private 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 
end 

從它聽起來像@micropost心不是在高清下的microposts_controller.rb文件初始化錯誤顯示。但我認爲這是?我究竟做錯了什麼?由於

也在這裏是應用程序/視圖/用戶/ show.html.erb

<% provide(:title, @user.name) %> 
<div class="row"> 
    <aside class="span4"> 
    <section> 
     <h1> 
     <%= gravatar_for @user %> 
     <%= @user.name %> 
     </h1> 
    </section> 
    </aside> 
    <div class="span8"> 
    <%= render 'follow_form' if signed_in? %> 
    <% if @user.microposts.any? %> 
     <h3>Microposts (<%= @user.microposts.count %>)</h3> 
     <ol class="microposts"> 
     <%= render @microposts %> 
     </ol> 
     <%= will_paginate @microposts %> 
    <% end %> 
    </div> 
</div> 
+0

你有一個UsersController嗎?按照慣例,我希望用戶/(user_id_#)在那裏被路由。 –

+0

我剛剛添加了users_controller.rb。我應該以某種方式在def show中定義評論方法嗎?我該怎麼寫呢?像@comment = @ micropost.comments.create(params [:comment])這樣的行會起作用嗎? – BigBoy1337

回答

1

我懷疑錯誤是在你這裏部分:

<% @micropost.comments.each do |comment| %> 

我假設你迭代超過@microposts並將每個微帖傳遞給您的部分微博。在上面的行中,您使用的是實例變量@micropost,當您應該使用本地微型號

更新:和這裏:

<%= form_for([@micropost, @micropost.comments.build]) do |f| %> – 
+0

嗯我改變@micropost只是micropost和得到相同的錯誤。任何其他想法? – BigBoy1337

+0

是的。你必須這樣做:<%= form_for([@ micropost,@ micropost.comments.build])do | f | %> - –

+0

好的,我把@關掉了這兩個,然後在用戶控制器的def show下添加@ comment = Comment.new。現在它告訴我SQLite3 :: SQLException:沒有這樣的列:comments.micropost_id:SELECT「comments」。* FROM「comments」WHERE「comments」。「micropost_id」= 1 – BigBoy1337

0

你能嘗試改變這種@microposts = @user.microposts.paginate(page: params[:page])這個@micropost = @user.microposts.first,看看它是否起作用? 我認爲,因爲部分是在用戶環境中呈現#顯示爲什麼@micropost是零。

這不是一個答案,我只是想評論,但我沒有足夠的聲望點。

+0

有趣。做完這些之後,所有豐富的鼓樂人都回答上面的問題,它似乎遇到了第16行而不是13的新錯誤。這是錯誤:'nil'不是ActiveModel兼容的對象,它返回有效的部分路徑。林不知道如果不同的錯誤意味着第一個是固定的 – BigBoy1337

+0

你可以發佈你的show.html.erb? – v4r

+0

我只是做了...... – BigBoy1337

相關問題