Created
November 6, 2013 06:26
-
-
Save regonn/7331792 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemsController < ApplicationController | |
before_action :set_item, only: [:show, :edit, :update, :destroy] | |
def index | |
@items = Item.all | |
end | |
def show | |
end | |
def new | |
@item = Item.new | |
end | |
def edit | |
end | |
def create | |
@item = Item.new(item_params) | |
respond_to do |format| | |
if @item.save | |
format.html { redirect_to @item, notice: 'Item was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @item } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @item.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def update | |
respond_to do |format| | |
if @item.update(item_params) | |
format.html { redirect_to @item, notice: 'Item was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @item.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@item.destroy | |
respond_to do |format| | |
format.html { redirect_to items_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def set_item | |
@item = Item.find(params[:id]) | |
end | |
def item_params | |
params.require(:item).permit(:name, :price, :description) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment