Admin not working


#1

Hey guys. Just did the admin addition to the recipe app.

I don't see the "Destroy" or "Delete" on the recipe page.

Here is my code!
app/views/recipes/show.html.erb:

<div class="recipe-show">
  <div class="container">
    <h2><%= @recipe.name %></h2>
    <img class="arrow" src="http://s3.amazonaws.com/codecademy-content/courses/rails-auth/img/arrow.svg" width="80" height="40">
    <div class="img_container">
      <%= image_tag @recipe.image %>
    </div>
    <div class="recipe-info">
      <p class="ingredients"><%= @recipe.ingredients %></p>
      <p class="instructions"><%= @recipe.instructions %></p> 
    </div> 
    
    <!-- Add links here -->
    <% if current_user && current_user.admin? %> 
      <p class="recipe-delete"><%= link_to "Delete", recipe_path(@recipe), method: "delete" %><p> 
    <% end %>
    <% if current_user && current_user.editor? %> 
      <p class="recipe-edit"> 
        <%= link_to "Edit Recipe", edit_recipe_path(@recipe.id) %> 
      </p> 
    <% end %>

  </div>
</div>

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

   helper_method :current_user
 
   def current_user
     @current_user ||= User.find(session[:user_id]) if session[:user_id]
   end 

   def require_user
     redirect_to '/login' unless current_user
   end 
   
   def require_editor 
	  redirect_to '/' unless current_user.editor? 
	end
  
  def require_admin
    redirect_to '/' unless current_user.admin?
  end
 
   

end

app/controllers/recipe_controller

class RecipesController < ApplicationController

  before_action :require_user, only: [:show, :edit, :update, :destroy]
  before_action :require_editor, only: [:show, :edit]
  before_action :require_admin, only: [:destroy]

  def show
    @recipe = Recipe.find(params[:id])
  end

  def edit
    @recipe = Recipe.find(params[:id])
  end

  def update
    @recipe = Recipe.find(params[:id])
      if @recipe.update(recipe_params)
        redirect_to @recipe
      else
        render 'edit'
      end
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy
    redirect_to root_url
  end 

  private
    def recipe_params
      params.require(:recipe).permit(:name, :ingredients, :instructions)
    end

end

#2

I know it's specifically to do with the "admin", becuase I tried switchign to an editor account and swapping the tags for both editor and they both display.
When I swap them both to admin and view on an admin account, nothing appears


#3

class User < ActiveRecord::Base

  has_secure_password
  
  def editor? 
	  self.role == 'editor' 
	end
	
	def admin? 
	  self.role == 'admin' 
	end

end

This is my users.rb in app/models


#4

Hello? Has anybody seen this? Please help! :stuck_out_tongue:


#5

I changed

    <% if current_user  && current_user.admin? %> 
      <p class="recipe-delete"><%= link_to "Delete", recipe_path(@recipe), method: "delete" %><p> 
    <% end %>

to

    <% if current_user %> 
      <p class="recipe-delete"><%= link_to "Delete", recipe_path(@recipe), method: "delete" %><p> 
    <% end %>

and it "delete" shows for Editor? user Mateo now. Though nothing still shows for Freida.

Which leaves me to believe that despite being logged in as Freida, for whatever reason, it's that account as a user at all?


#6

Hey @kvy,

Your code looks fine to me, so I'm not sure what's wrong. Could you open up a Rails console in the terminal, then view all users to see if one of them actually is an admin? You can do that by typing this in the terminal:

$ rails console
> User.all

And please copy/paste the output into a post here so I can see it too :slight_smile:


#7

irb(main):006:0> User.all                                                                                                                                
  User Load (0.2ms)  SELECT "users".* FROM "users"                                                                                                       
=> #<ActiveRecord::Relation [#<User id: 1, first_name: "Mateo", last_name: "Lazo", email: "mateo@email.com", password_digest: "$2a$10$ZjeNimYeyVH0Akvy/wZ
GF.Frr5A5BTtAnnB5H3r39Ai...", role: "editor", created_at: "2016-09-14 19:15:00", updated_at: "2016-09-14 19:15:00">, #<User id: 2, first_name: "Julian", 
last_name: "Jones", email: "julian@email.com", password_digest: "$2a$10$aqGeNcCOCEGk2g.WeYaFweZ3dAXLZ3lPxfJizChMxFJ...", role: nil, created_at: "2016-09-
14 19:15:00", updated_at: "2016-09-14 19:15:00">, #<User id: 3, first_name: "Freida", last_name: "Gray", email: "freida@email.com", password_digest: "$2a
$10$GkMdYCQv/jzCnLYpMQCese8fbqhmmPQIGPR5fDRPKHf...", role: "admin", created_at: "2016-09-14 19:15:00", updated_at: "2016-09-14 19:15:00">, #<User id: 4, 
first_name: "Mateo", last_name: "Lazo", email: "mateo@email.com", password_digest: "$2a$10$BKuYQLXom6J50/kSzydSUuXPHh2UyHs6O0tKdd6eO03...", role: "editor
", created_at: "2016-09-14 19:15:08", updated_at: "2016-09-14 19:15:08">, #<User id: 5, first_name: "Julian", last_name: "Jones", email: "julian@email.co
m", password_digest: "$2a$10$40qxXtOx0uJce4cVht7GAea3A21p5F3Lc9kQXaMWpvp...", role: nil, created_at: "2016-09-14 19:15:08", updated_at: "2016-09-14 19:15
:08">, #<User id: 6, first_name: "Freida", last_name: "Gray", email: "freida@email.com", password_digest: "$2a$10$qI/InAVMkqXJg5WEgp.o7uIpOnYWRxjRKrTmtW8
wdIv...", role: "admin", created_at: "2016-09-14 19:15:08", updated_at: "2016-09-14 19:15:08">, #<User id: 7, first_name: "Mateo", last_name: "Lazo", ema
il: "mateo@email.com", password_digest: "$2a$10$pL4Bw4ipoQ08HI7L.KvmXeIxnQIasWnMmkCYwLDtwv/...", role: "editor", created_at: "2016-09-14 19:21:02", updat
ed_at: "2016-09-14 19:21:02">, #<User id: 8, first_name: "Julian", last_name: "Jones", email: "julian@email.com", password_digest: "$2a$10$IJJop7sGFkIyY.
7eE9QaO..zVm7KVSVEXHFpMUh.U3Y...", role: nil, created_at: "2016-09-14 19:21:02", updated_at: "2016-09-14 19:21:02">, #<User id: 9, first_name: "Freida", 
last_name: "Gray", email: "freida@email.com", password_digest: "$2a$10$GDmNSOPFngKD7xvCabDCxesrl1XiGCgkpohAwqmvBwt...", role: "admin", created_at: "2016-
09-14 19:21:02", updated_at: "2016-09-14 19:21:02">]>

This is the full response.

I had previously done the test

freida = User.find_by(email: 'freida@email.com')

freida.admin?

Got response true. But you make a call here. Lol


#8

Also, if I change the "Delete" to show to editors instead, and login to editor account, it shows up for them.
Though I changed all of them to show to admins, and on the admin account nothing shows.
Very strange..


#9

@kvy If you add :show to the list of things that use :require_admin as a before action in the controller, are you able to view a recipe?

before_action :require_admin, only: [:show, :destroy]

#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.