My image size check, doesn't work right. it works in update action, but doesn't work in create Maybe problem is .build(real_params)
I have model real estate
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
Controller with update and create actions
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
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
This is my errors render
<% 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 %>
Aucun commentaire:
Enregistrer un commentaire