6.Admin Roll II Edit Function Issue


#1



https://www.codecademy.com/courses/rails-auth/lessons/authorization/exercises/authorization-admin-role-ii?action=lesson_resume


I have an issue with with my code.I am currently unable to see the edit function within the browser when viewing the site.my code has passed and I am logged in as Freida per the instructions.I have included a screenshot of the page.Any feedback would be appreciated.
thanks;
Glenn
Glenn

here is my code

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: [:show, :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

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.editor? %> 
		  <p class="recipe-edit"> 
		    <%= link_to "Edit Recipe",
     edit_recipe_path(@recipe.id) %>  
		  </p> 
		<% end %>
		
		<% if current_user && current_user.admin? %> 
		  <p class="recipe-delete">
        <%= link_to "Delete", recipe_path(@recipe), method: "delete" %>
      </p> 
		<% end %>

  </div>
</div>


#2

Hi @thenotoriousg,

With the three before_actions, we have three qualifications we must meet to view a recipe:

  1. Be a registered user :white_check_mark:
  2. Be an editor :x:
  3. Be an admin :white_check_mark:

As you can see, since the freida user is an admin, it meets the first and third qualifications, but not the second. Try removing the :show from the second and third before_action, and it should work then :slight_smile:


#3

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