Sha256: 9fefbfbc77f9093619cbd65825b4e2a1d6f8efd08a678da61d392c7f3f23e193
Contents?: true
Size: 1.67 KB
Versions: 13
Compression:
Stored size: 1.67 KB
Contents
class PostsController < ApplicationController before_action :set_post, only: %i[ edit update destroy ] def index @posts = Post.all end # GET /posts/1 or /posts/some-friendly-slug def show @post = Post.friendly.find(params[:id]) @friendly = @post.slug == params[:id] end def new @post = Post.new end def edit end def create @post = Post.new(post_params) respond_to do |format| if @post.save format.html { redirect_to post_url(@post), notice: "Post was successfully created." } format.json { render :show, status: :created, location: @post } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @post.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @post.update(post_params) format.html { redirect_to post_url(@post), notice: "Post was successfully updated." } format.json { render :show, status: :ok, location: @post } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @post.errors, status: :unprocessable_entity } end end end def destroy @post.destroy respond_to do |format| format.html { redirect_to posts_url, notice: "Post was successfully destroyed." } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a list of trusted parameters through. def post_params params.require(:post).permit(:title, :body) end end
Version data entries
13 entries across 13 versions & 1 rubygems