Created
October 4, 2016 14:56
-
-
Save eric1234/951724ea631da0cb06b5b400ec02ebf4 to your computer and use it in GitHub Desktop.
Stock Scaffold Rails Controller
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 WidgetsController < ApplicationController | |
before_action :set_widget, only: [:show, :edit, :update, :destroy] | |
# GET /widgets | |
def index | |
@widgets = Widget.all | |
end | |
# GET /widgets/1 | |
def show | |
end | |
# GET /widgets/new | |
def new | |
@widget = Widget.new | |
end | |
# GET /widgets/1/edit | |
def edit | |
end | |
# POST /widgets | |
def create | |
@widget = Widget.new(widget_params) | |
if @widget.save | |
redirect_to @widget, notice: 'Widget was successfully created.' | |
else | |
render :new | |
end | |
end | |
# PATCH/PUT /widgets/1 | |
def update | |
if @widget.update(widget_params) | |
redirect_to @widget, notice: 'Widget was successfully updated.' | |
else | |
render :edit | |
end | |
end | |
# DELETE /widgets/1 | |
def destroy | |
@widget.destroy | |
redirect_to widgets_url, notice: 'Widget was successfully destroyed.' | |
end | |
private | |
# Use callbacks to share common setup or constraints | |
# between actions. | |
def set_widget | |
@widget = Widget.find(params[:id]) | |
end | |
# Only allow a trusted parameter "white list" through. | |
def widget_params | |
params.require(:widget).permit(:name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment