Search code examples
ruby-on-railsdeviseadminmodel-associations

one month rails user specific routes and pin controller


I want to alter the path generated by my pins to accommodate an admin user to access another users pins and be able to delete them.

I have created a user model with devise and added the admin attribute. This works fine but since I have associated each pin with current_user in the controller I cannot delete others or view pins while logged in as an admin user.

I think the problem lies here in my pins_controller.rb

@pin = current_user.pins.find(params[:id])

Below is the actual route which isn't working

<% if current_user == pin.user or user_signed_in? && current_user.admin? %>
    <p>
      <%= link_to content_tag(:i, "", class:"icon-edit"), edit_pin_path(pin) %> |
      <%= link_to content_tag(:i, "", class:"icon-trash"), pin, method: :delete, data: { confirm: 'Are you sure?' } %>
    </p>
<% end %> 

Also I could be wrong about this and the problem might be in the routes.rb file

Omrails::Application.routes.draw do

get "users/show"

resources :pins

devise_for :users
match 'users/:id' => 'users#show', as: :user

get 'about' => 'pages#about'

root :to => 'pins#index'

my git repository is here: https://github.com/nathan-wallace/imageapp.git


Solution

  • you could try this

        <% if user_signed_in? %>
          <% if (current_user == @pin.user) or current_user.admin? %>
          <p>
            <%= link_to content_tag(:i, "", class:"icon-edit"), edit_pin_path(pin) %> |
            <%= link_to content_tag(:i, "", class:"icon-trash"), pin, method: :delete, data: {               confirm: 'Are you sure?' } %>
          </p>
         <% end %>
        <% end %>