Search code examples
ruby-on-railspathposts

<%= posts_delete_path %> dont work


I'm programming a website with a friend, and I'm getting one error with my code. In the computer of my friend, he have the same code and don't have any error.

I get the following error:

We're sorry, but something went wrong.

Well, we're newbie in Rails and I get this error when I try to load the posts page. We think the error is on deleting a post, because if we comment the line of "posts_delete_path", everything works. For delete a post, we are using:

show.hmtl.erb:

<% @hashtag.posts.each do |p| %>
    <li>Posted by: <%= p.user.name %></li>
    <li>Content:</li>
    <li><%= p.content %></li>
    <li><a href="<%= posts_delete_path %>/<%= p.id %>" >Delete</a></li>
<% end %>

routes:

   match '/signout', to: 'sessions#destroy'
   match 'posts/delete/:id', to: 'posts#destroy'
   resources :posts, :only => [:create, :show]

posts controller:

def destroy
    @post = Post.find(params[:id])
    if current_user.id == @post.user_id
       @post.destroy
    end
end

---- edit ---- rake routes:

      posts GET    /posts(.:format)           posts#index
            POST   /posts(.:format)           posts#create
   new_post GET    /posts/new(.:format)       posts#new
  edit_post GET    /posts/:id/edit(.:format)  posts#edit
       post GET    /posts/:id(.:format)       posts#show
            PUT    /posts/:id(.:format)       posts#update
            DELETE /posts/:id(.:format)       posts#destroy

We know that's the newbie way to delete a post, but we don't find any better solution. (If you want suggest something, we will be glad too).


Solution

  • Thanks for everyone who tried help me. Now I solved my problem, but by other way.

    What I do is:

    In view:

    <% if current_user.id == p.user_id %>
                <%= button_to "Delete", { :controller => :posts, :action => 'destroy', :id => p.id }, :method => :delete %>
    <% end %>
    

    In controller:

      def destroy
        @post = Post.find(params[:id])
        #if current_user.id == @post.user_id
           @post.destroy
        end
      end
    

    In routes:

    resources :posts
    

    Thanks guys!