【问题标题】:Carrierwave - upload and display new image at firstCarrierwave - 首先上传并显示新图像
【发布时间】:2017-01-19 05:18:47
【问题描述】:

我在我的 ruby​​ on rails 应用程序中使用carrierwave 上传器。当我上传图片时,该图片会在图片库中获得最后一张。如何让我上传的新图片排在所有图片的首位?

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

图片 new.html.erb

<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 %>

图像 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

【问题讨论】:

  • 您能否显示您收集所有图像的代码。我认为您必须在获取图像时下订单。
  • 能否请您显示显示页面的控制器代码。

标签: ruby-on-rails carrierwave


【解决方案1】:

把它放在你的 images_controller.rb 中

def show @images = Image.all.order('id desc') end

它会首先显示最后一张图片。

【讨论】:

  • 但是,这不适用于 uuid 主键模型。在这些情况下,您应该创建一个新列来根据您的排序和排序。
  • 你也可以Image.all.order('created_at desc')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 2015-11-29
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多