Created
July 29, 2014 19:55
-
-
Save ahimmelstoss/05618329e1d86aee383b to your computer and use it in GitHub Desktop.
Listings 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 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