Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.86 KB | None | 0 0
  1. class MoviesController < ApplicationController
  2.   before_action :set_movie, only: [:show, :edit, :update]
  3.  
  4.   def index
  5.     @movies = Movie.all
  6.   end
  7.  
  8.   def show
  9.   end
  10.  
  11.   def edit
  12.   end
  13.  
  14.   def update
  15.     @movie.update(movie_params)
  16.     if @movie.save
  17.       redirect_to action: :show, id: @movie.id
  18.     else
  19.       render :edit, id: @movie.id
  20.     end
  21.   end
  22.  
  23.   def new
  24.     @movie = Movie.new
  25.   end
  26.  
  27.   def create
  28.     @movie = Movie.new(movie_params)
  29.     if @movie.save
  30.       redirect_to action: :show, id: @movie.id
  31.     else
  32.       render :new
  33.     end
  34.   end
  35.  
  36.   private
  37.  
  38.   def movie_params
  39.     params.require(:movie).permit(:title, :release_date, :description)
  40.   end
  41.  
  42.   def set_movie
  43.     id = params['id']
  44.     @movie = Movie.find(id)
  45.   rescue ActiveRecord::RecordNotFound
  46.     render file: "#{Rails.root}/public/404.html", status: 404
  47.   end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement