'NameError uninitialized class variable @@gallery in > PropertiesController Did you mean? caller
after setup a image gallery for post the logs spit out this
NameError (uninitialized class variable @@gallery in PropertiesController Did you mean? caller):
Issue
So i tried to remove a @ from the variable! the thing is just a image galler for the post, and i received the code with 2 @@ as im new to ruby this context using 2 @@ for variable, why?
but still with the issue, please someone can spare a hint?
Post controller
class PropertiesController < ApplicationController
before_action :authenticate_admin!, except: [:home, :index, :show]
include ApplicationHelper
def show
@property = Property.find(params[:id])
@question = params[:question]
end
def new() initialize_form end
def edit() initialize_form end
def get_images() render json: {images: @@gallery.images} end
def create
@property = property_type.new property_params
@property.gallery = @@gallery
unmask_fields
if @property.save
@@gallery.save if @@gallery
redirect_to @property, flash: { notice: "Criado com sucessso!" }
else
render "properties/new_or_edit"
end
end
def add_image
images = @@gallery.images
images << image_param[:image]
@@gallery.images = images
if @@gallery.valid?
# if @@gallery.save
render json: {images: @@gallery.images}
# end
else
render json: @@gallery.errors.messages, status: 406
@@gallery.images.pop
end
end
def remove_image
remain_images = @@gallery.images
deleted_image = remain_images.delete_at(params[:index].to_i) # delete the target image
@@gallery.images = remain_images
if @@gallery.valid?
if @@gallery.save
render json: {images: @@gallery.images}
deleted_image.try(:remove!) # delete image from S3
end
else
render json: @@gallery.errors.messages, status: 400
end
end
def set_main_image
images = @@gallery.images
images.insert(0, images.delete_at(params[:index].to_i))
@@gallery.images = images
if @@gallery.valid?
render json: {images: @@gallery.images}
else
render json: @@gallery.errors.messages, status: 400
end
end
def initialize_form
@property = property_type.find(params[:id]) if params[:id]
@property ||= property_type.new
@@gallery = @property.gallery || Gallery.new
render "properties/new_or_edit"
end
private
def property_params
params.require(params[:type].downcase.to_sym).permit PROPERTY_PARAMS
end
def image_param
params.permit(:image)
end
def property_id
if params[:type] then params["#{params[:type].downcase}_id".to_sym] else nil end
end
def property_name
"#{params[:type].downcase}"
end
def property_type
params[:type].constantize if params[:type].in? PROPERTY_TYPES
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
