Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rails.application.routes.draw do
- resources :surroundings
- resources :headings
- resources :options
- resources :categories
- resources :mains
- resources :properties do
- resources :images, only: [:index, :new, :create, :destroy] do
- collection do
- get 'update_main'
- end
- end
- get 'addoption'
- post 'postoption'
- end
- root 'search#main'
- get 'main' =>'search#main'
- class ImagesController < ApplicationController
- def new
- redirect_to property_images_path #used for trouble shooting - does not redirect
- @image = Image.new
- end
- def index
- @images = Property.find(params[:property_id]).images
- end
- def update_main
- #set the current main image to false
- r=Property.find(params[:property_id]).images.where("main" => true).first
- r.main=false unless r.nil?
- r.save unless r.nil?
- @image=Image.find(params[:main])
- @image.main=true;
- @image.save
- redirect_to property_images_path, notice: "The Image has been uploaded."
- end
- def create
- @image = Image.new(image_params)
- @image.property_id=params[:property_id]
- if @image.save
- redirect_to property_images_path, notice: "The Image has been uploaded."
- else
- render "new"
- end
- end
- def destroy
- @image = Image.find(params[:id])
- @image.destroy
- redirect_to property_images_path, notice: "The Image has been deleted."
- end
- private
- def image_params
- params.require(:image).permit(:attachment,:property_id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement