2012-09-22 22:39:12 -04:00
|
|
|
class ItemsController < ApplicationController
|
|
|
|
|
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update]
|
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
|
|
|
# GET /items
|
|
|
|
def index
|
|
|
|
@user = current_user
|
|
|
|
@items = Item.all
|
|
|
|
|
|
|
|
respond_with(@items)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get /item/new
|
|
|
|
def new
|
|
|
|
@item = Item.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /item/:id
|
|
|
|
def show
|
|
|
|
@item = Item.find(params[:id])
|
|
|
|
|
2012-10-08 23:18:00 -04:00
|
|
|
@relatives = @item.as_json.html_safe
|
2012-09-22 22:39:12 -04:00
|
|
|
|
2012-10-08 23:18:00 -04:00
|
|
|
respond_to do |format|
|
2012-10-17 20:51:54 -04:00
|
|
|
format.html { respond_with(@item) }
|
2012-10-08 23:18:00 -04:00
|
|
|
format.json { respond_with(@relatives) }
|
|
|
|
end
|
2012-09-22 22:39:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /items
|
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
@item = Item.new()
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
|
|
|
@item.item_category = ItemCategory.find(params[:category])
|
|
|
|
@item.user = @user
|
|
|
|
|
|
|
|
@item.save
|
|
|
|
|
|
|
|
respond_to do |format|
|
2012-10-09 20:23:45 -04:00
|
|
|
format.html { respond_with(@user, location: item_url(@item)) }
|
2012-09-22 22:39:12 -04:00
|
|
|
format.js { respond_with(@item) }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /items/:id/edit
|
|
|
|
def edit
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
2012-10-18 09:33:16 -04:00
|
|
|
respond_with(@item)
|
2012-09-22 22:39:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /actions/:id
|
|
|
|
def update
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
2012-09-23 21:30:48 -04:00
|
|
|
if @item
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
2012-09-29 23:45:14 -04:00
|
|
|
@item.item_category = ItemCategory.find(params[:category][:item_category_id])
|
2012-09-23 21:30:48 -04:00
|
|
|
|
|
|
|
@item.save
|
|
|
|
end
|
|
|
|
|
2012-10-09 20:23:45 -04:00
|
|
|
respond_with(@user, location: item_url(@item)) do |format|
|
2012-09-22 22:39:12 -04:00
|
|
|
end
|
2012-10-08 01:45:44 -04:00
|
|
|
|
2012-09-22 22:39:12 -04:00
|
|
|
end
|
|
|
|
|
2012-10-09 20:23:45 -04:00
|
|
|
# DELETE /items/:id
|
2012-09-22 22:39:12 -04:00
|
|
|
def destroy
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
|
|
|
@item.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|