Reputation: 377
I'm using carrierwave uploader in my ruby on rails application. When i upload image, that image get the last one in image gallery. How to do that my new uploaded image go to first place of all images?
images_controller.rb
def create
@image = Image.new(image_params)
if @image.save
flash[:notice] = "Image Created"
redirect_to :back
else
render 'new'
end
end
images new.html.erb
<h2 class="upload-image-header">Upload Image</h2>
<%= form_for @image, :html => {:multipart => true} do |f| %>
<!-- Check for error -->
<% [email protected]? %>
<% @image.errors.full_messages.each do |msg| %>
<!-- Show errors -->
<div id="alert alert-danger"><%= msg %></div>
<% end %>
<% end %>
<div class="from-group">
<%= f.label :image_title %>
<%= f.text_field :image_title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|x| [x.name, x.id]}, {:include_blank => 'Select One'}, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :image_description %>
<%= f.text_area :image_description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<div class="form-group">
<%= f.label :remote_image_url, "or image url" %>
<%= f.text_field :remote_image_url, class: "form-control" %>
</div>
<br>
<%= f.submit "Submit", class: "btn btn-primary" %>
<%= link_to "Cancel", images_path, class: "btn btn-default" %>
<% end %>
images show.html.erb
<% if @images.exists? %>
<% @images.each_slice(4) do |image| %>
<div class="row">
<% image.each do |image| %>
<div class="col-md-3 col-xs-12 col-sm-6 images-column">
<center><div class="headline"><%= image.image_title %></div></center>
<div class="photo">
<a href="<%= image.image_url() %>" class="photo-hover img-responsive" data-lightbox="my-images" data-title="<%= image.image_title %>">
<div class="mask"></div>
</a>
<div class="photo-img"><%= image_tag image.image_url(:thumb) unless image.image.blank? %></div>
</div>
<center><div class="description"><%= image.image_description %></div></center>
</div>
<% end %>
</div>
<% end %>
</div>
</div>
categories_controller.rb
def show
@category = Category.find(params[:id])
@categories = Category.all
@images = @category.images.page(params[:page]).per_page(8)
end
Upvotes: 0
Views: 787
Reputation: 530
Put this in your images_controller.rb
def show
@images = Image.all.order('id desc')
end
It will show the last image first.
Upvotes: 1