0

I am studying Rails and I have a demo app to develop, on which I installed Devise and CanCanCan for authentication and authorization. I have some Products and I just added the comments functionality. If I comment a product, it won't let me create one, an error appears with "You are not authorized to access this page" and then it redirects me to my first page (landing_page). I have the same issue when trying to delete a comment. (Comment has been set as a nested resource of product) The following is the comments view I am using

    <div class="product-reviews">

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

         <div class="row" style="padding-left:4%;">
           <HR>
               <p><small><%= comment.user.email %><em><%= " #{time_ago_in_words(comment.created_at)} ago" %></em></small></p>
            <div class="rated" data-score="<%= comment.rating %>"></div>

            <p><%= comment.body %></p>

            <% if signed_in? && current_user.admin? %> 
                <p><%= link_to 'Destroy', product_comment_path( @product, comment), method: :delete, data: { confirm: 'Are you sure?'} %></p>
            <% end %>

    </div>
<% end %>

This is also the link to my github https://github.com/Adsidera/FreshObst

I am hitting my head on the routes and I cannot figure out where the mistake is, whether in the products, or comments controller or it is a routes.rb issue. Thanks a lot in advance! Anna

Anna Costalonga
  • 180
  • 2
  • 8
  • 1
    Looking at your `ability.rb` file on the repo (it would probably be good to paste its contents into this question), you are not setting any specific abilities for the `Product` and `Comment` model. If you added `can :manage, Comment` and `can :manage, Product`, does it change the behavior? Not saying that will or should resolve the issue; rather, just looking to help narrow down the scope of your issue! – craig.kaminsky Feb 04 '16 at 20:32
  • 1
    sounds like CanCan is interfering with the route for creating comments. can you post some controller/model code so we can check it out? – Andrew Hendrie Feb 04 '16 at 20:34
  • Thanks it seems it was an issue with CanCanCan and its ability.rb – Anna Costalonga Feb 05 '16 at 12:34
  • I just reviewed step by step the lessons I was following and could solve that issue. Now I can add comments, and they get displayed, and I can also destroy the comments...the issue is changed though, because after clicking for confirmation, I get the error `ActiveRecord::RecordNotFound in ProductsController#show Couldn't find Product with 'id'=26 Rails.root: C:/Users/Adsidera/FreshObst Application Trace | Framework Trace | Full Trace app/controllers/products_controller.rb:103:in `set_product' ` – Anna Costalonga Feb 05 '16 at 12:37

1 Answers1

0

I just reviewed step by step the lessons I was following and could solve that issue. Now I can add comments, and they get displayed, and I can also destroy the comments..

Anna Costalonga
  • 180
  • 2
  • 8