Ruby on rails Carrierwave-首先上载并显示新图像
我在ruby on rails应用程序中使用carrierwave上载程序。当我上传图像时,该图像将成为图像库中的最后一个图像。如何让我新上传的图片排在所有图片的第一位 images\u controller.rbRuby on rails Carrierwave-首先上载并显示新图像,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,我在ruby on rails应用程序中使用carrierwave上载程序。当我上传图像时,该图像将成为图像库中的最后一个图像。如何让我新上传的图片排在所有图片的第一位 images\u controller.rb def create @image = Image.new(image_params) if @image.save flash[:notice] = "Image Created" redirect_to :back else
def create
@image = Image.new(image_params)
if @image.save
flash[:notice] = "Image Created"
redirect_to :back
else
render 'new'
end
end
<h2 class="upload-image-header">Upload Image</h2>
<%= form_for @image, :html => {:multipart => true} do |f| %>
<!-- Check for error -->
<% if@image.errors.any? %>
<% @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 %>
<% 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>
def show
@category = Category.find(params[:id])
@categories = Category.all
@images = @category.images.page(params[:page]).per_page(8)
end
images new.html.erb
def create
@image = Image.new(image_params)
if @image.save
flash[:notice] = "Image Created"
redirect_to :back
else
render 'new'
end
end
<h2 class="upload-image-header">Upload Image</h2>
<%= form_for @image, :html => {:multipart => true} do |f| %>
<!-- Check for error -->
<% if@image.errors.any? %>
<% @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 %>
<% 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>
def show
@category = Category.find(params[:id])
@categories = Category.all
@images = @category.images.page(params[:page]).per_page(8)
end
把这个放到你的images\u controller.rb里
def显示
@images=Image.all.order('id desc')
结束
它将首先显示最后一张图像。请显示收集所有图像的代码。我认为在获取图像时您必须下订单。请您显示显示显示页面的控制器代码。但是,这不适用于uuid主键模型。在这种情况下,您应该创建一个新的列来根据它进行排序和排序。您还可以执行
Image.all.order('created_at desc')