Skip to content

Instantly share code, notes, and snippets.

@siddartha
Created November 27, 2012 15:19
Show Gist options
  • Save siddartha/4154757 to your computer and use it in GitHub Desktop.
Save siddartha/4154757 to your computer and use it in GitHub Desktop.
Categorie
<%= simple_form_for(@category) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :parent_id %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
class Categorie < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :description, :name, :parent_id
validates_presence_of :name
validates_uniqueness_of :name, :case_sensitive => false
end
class CategoriesController < ApplicationController
# GET /categories
# GET /categories.json
def index
@categories = Categorie.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @categories }
end
end
# GET /categories/1
# GET /categories/1.json
def show
@category = Categorie.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @category }
end
end
# GET /categories/new
# GET /categories/new.json
def new
@category = Categorie.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @category }
end
end
# GET /categories/1/edit
def edit
@category = Categorie.find(params[:id])
end
# POST /categories
# POST /categories.json
def create
@category = Categorie.new(params[:category])
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Categorie was successfully created.' }
format.json { render json: @category, status: :created, location: @category }
else
format.html { render action: "new" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
# PUT /categories/1
# PUT /categories/1.json
def update
@category = Categorie.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
format.html { redirect_to @category, notice: 'Categorie was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category = Categorie.find(params[:id])
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url }
format.json { head :no_content }
end
end
end
<h1>New category</h1>
<%= render 'form' %>
<%= link_to 'Back', categories_path %>
Annuaire::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users, :categories
end
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @category.name %>
</p>
<p>
<b>Description:</b>
<%= @category.description %>
</p>
<p>
<b>Parent:</b>
<%= @category.parent_id %>
</p>
<%= link_to 'Edit', edit_category_path(@category) %> |
<%= link_to 'Back', categories_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment