문제

I'm working through the rails tutorial and have run into an error.

$ bundle exec rspec spec

Failure/Error: expect { click_link "delete" }.should change(Micropost, :count).by(-1)
   NoMethodError:
   undefined method `destroy' for nil:NilClass

There are two errors, caused by this same issue of an undefined method 'destory' for nil:NilClass

Micropost controller

class MicropostsController < ApplicationController
before_filter   :signed_in_user,    only: [:create, :destory]
before_filter   :correct_user,      only: :destory
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 destroy
    @micropost.destroy
    redirect_to 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

I think that is all the information that is needed to help me figure out this issue. Here is some more anyways.

Related question: Rails request spec not working - noMethodError

I tried their solution, but it made no difference.

_feed_item.html.erb

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

Thanks

도움이 되었습니까?

해결책

You have a typo:

before_filter :correct_user, only: :destory

:destory should be :destroy, this is causing @micropost to be nil. You make the same spelling error in the other before_filter btw.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top