Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AdsController < ApplicationController
- before_action :set_ad, only: [ :show, :edit, :update, :destroy ] #отделный метод для @ad = Ad.find(params[:id]), чтобы его не дублировать
- def index
- @ads = Ad.order(created_at: :desc) #вывод всех постов из бд, отсортированных по дате создания
- end
- def show
- end
- def new
- @ad = Ad.new
- end
- def edit
- end
- def update
- if @ad.update_attributes(ad_params)
- redirect_to @ad
- else :edit
- end
- end
- def destroy
- @ad.destroy
- redirect_to ads_path
- end
- def create
- @ad = Ad.new(ad_params)
- if @ad.save
- redirect_to @ad
- else
- render :new
- end
- end
- private
- def ad_params
- params.require(:ad).permit(:title, :summary, :body, :image)
- end
- def set_ad
- @ad = Ad.find(params[:id])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement