Skip to content

Scoped Controller Example

Brad Potts edited this page Jul 9, 2015 · 1 revision

Here is a simple scoped exanple controller.

class DirectorycategoriesController < ApplicationController  

  # INDEX  
  def index  
    @pages = Page.scoped_to(current_account)  
  end  

  # SHOW  
  def show  
    @page = Page.scoped_to(current_account).find(params[:id])  
  end 

  # NEW  
  def new  
    @page = Page.scoped_to(current_account).new  
  end  

  # EDIT  
  def edit  
  end  

  # CREATE  
  def create  
    @page = Page.scoped_to(current_account).new(page_params)  
    respond_to do |format|  
      if @page.save  
        format.html { redirect_to page_url, notice: 'Page was successfully created.' }  
        format.json { render :show, status: :created, location: @page }  
      else  
        format.html { render :new }  
        format.json { render json: @page.errors, status: :unprocessable_entity }  
      end  
    end  
  end  

  # UPDATE  
  def update  
    respond_to do |format|  
      if @page.update(page_params)  
        format.html { redirect_to pages_url, notice: 'Page was Successfully Updated.' }  
        format.json { render :show, status: :ok, location: @page }  
      else  
        format.html { render :edit }  
        format.json { render json: @page.errors, status: :unprocessable_entity }  
      end  
    end  
  end  

  # DELETE  
  def destroy  
    @page.destroy  
    respond_to do |format|  
      format.html { redirect_to pages_url, notice: 'Page was Successfully Destroyed.' }  
      format.json { head :no_content }  
    end  
  end  

end