Created
April 30, 2013 18:19
-
-
Save nchapman/5490742 to your computer and use it in GitHub Desktop.
Less than beautiful generated controllers in Rails 4.
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 OrganizationsController < ApplicationController | |
before_action :set_organization, only: [:show, :edit, :update, :destroy] | |
# GET /organizations | |
# GET /organizations.json | |
def index | |
@organizations = Organization.all | |
end | |
# GET /organizations/1 | |
# GET /organizations/1.json | |
def show | |
end | |
# GET /organizations/new | |
def new | |
@organization = Organization.new | |
end | |
# GET /organizations/1/edit | |
def edit | |
end | |
# POST /organizations | |
# POST /organizations.json | |
def create | |
@organization = Organization.new(organization_params) | |
respond_to do |format| | |
if @organization.save | |
format.html { redirect_to @organization, notice: 'Organization was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @organization } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @organization.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /organizations/1 | |
# PATCH/PUT /organizations/1.json | |
def update | |
respond_to do |format| | |
if @organization.update(organization_params) | |
format.html { redirect_to @organization, notice: 'Organization was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @organization.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /organizations/1 | |
# DELETE /organizations/1.json | |
def destroy | |
@organization.destroy | |
respond_to do |format| | |
format.html { redirect_to organizations_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_organization | |
@organization = Organization.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def organization_params | |
params.require(:organization).permit(:name, :street, :city, :state, :postal_code) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment