Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Scope "Destroy" method - Build a Simple Ruby on Rails App

Hi there,

How can we scope destroy method in order to make it only available for the current user statuses? When I try the same code that the one used to scope "update" and "create" it doesn't work.

Many thanks in advance!

2 Answers

Thank you Glenn! It helps me to solve my issue.

Here is my code:

def destroy
    if current_user.id == @status.user.id
      @status.destroy
    else
      flash[:error] = "Status could not be deleted"
    end
    redirect_to statuses_path
end

But I still don't understand why the following code (used to scope the update method) doesn't work with the destroy method? :

@status = current_user.statuses.find(params[:id])

Anyway thanks again Glenn.