Answer the question
In order to leave comments, you need to log in
How to render errors?
Good day. Can't check image size before sending.
There is a real_estate model
has_many :pictures, dependent: :destroy
validate :picture_size
def picture_size
pictures.each do |img|
if img.image_file_size > 5.megabytes
errors.add(:pictures, "should be less than 5MB")
end
end
end
def update
@user = current_user
@real = RealEstate.find(params[:id])
if @real.update(real_params)
if params[:images]
# The magic is here ;)
params[:images].each do |image|
@real.pictures.create(image: image)
end
end
redirect_to root_path
else
render 'edit'
end
end
def create
@user = current_user
@real = current_user.real_estates.build(real_params)
if @real.save
if params[:images]
# The magic is here ;)
params[:images].each do |image|
@real.pictures.create(image: image)
end
end
#flash[:success] = "Real Estate created!"
redirect_to root_path
else
render 'real_estates/new'
end
end
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
.build(real_params)
Answer the question
In order to leave comments, you need to log in
It seems to me that the model should be added before_create :check_picture_size
. Thus, the check will be performed before the image is created. If you want to be executed before saving, then replace with before_save
.
Of course, I could be wrong, but you first check for validation (if @real.save) and then the images are saved. With this implementation, pictures should not be saved, but there will be no errors about this either.
$.getJSON('ajax/goods.json',).then(function() {
console.log(data);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question