Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created July 29, 2014 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahimmelstoss/05618329e1d86aee383b to your computer and use it in GitHub Desktop.
Save ahimmelstoss/05618329e1d86aee383b to your computer and use it in GitHub Desktop.
Listings Controller
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :update, :destroy]
# GET /listings
def index
if listing_type = params[:listing_type]
@listings = Listing.where(listing_type: listing_type)
elsif price = params[:price]
@listings = Listing.where(price: price)
else
@listings = Listing.all
end
respond_to do |format|
format.json
end
end
# GET /listings/:id
def show
respond_to do |format|
format.json
end
end
# POST /listings
def create
@listing = Listing.new(listing_params)
if @listing.save
render json: @listing, status: 201, location: @listing
else
render json: @listing.errors, status: 422
end
end
# PATCH /listings/:id
def update
if @listing.update(listing_params)
render json: @listing, status: 200
else
render json: @listing.errors, status: 422
end
end
# DELETE /listings/:id
def destroy
@listing.destroy
head 204
end
private
def set_listing
@listing = Listing.find(params[:id])
end
def listing_params
params.require(:listing).permit(:address, :listing_type, :title, :description, :price, :neighborhood_id, :host_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment