Sha256: cfd88bf647bd433fb52013f357bd79b08ea0507d18cdf4ad9622cfec8efca220

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post
    @posts = @posts.where(user_id: params[:user_id]) if params[:user_id]
    @posts = @posts.all

    render json: @posts
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    render json: @post
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    if @post.save
      render json: @post, status: :created, location: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    if @post.update(post_params)
      head :no_content
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy

    head :no_content
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :body, :user_id)
    end
end

Version data entries

5 entries across 4 versions & 1 rubygems

Version Path
tests_doc-0.0.4 examples/rails-4.2.5/app/controllers/posts_controller.rb
tests_doc-0.0.4 examples/rails-5.2.3/app/controllers/posts_controller.rb
tests_doc-0.0.3 examples/rails-4.2.5/app/controllers/posts_controller.rb
tests_doc-0.0.2 examples/rails-4.2.5/app/controllers/posts_controller.rb
tests_doc-0.0.1 examples/rails-4.2.5/app/controllers/posts_controller.rb