Rails获取图像并在我的索引视图中显示

时间:2016-02-08 10:11:40

标签: ruby-on-rails carrierwave

我正在尝试构建一个rails应用程序,我在其中使用carrierwave上传图像。现在上传后我想检索该图像并显示在我的索引页面中,但我不知道如何指定图像的路径或如何检索该图像并在视图中显示它。我还想显示我上传的标题和描述等详细信息
[uploads_controller.rb]

class UploadsController < ApplicationController
  before_action :authenticate_user! 

  def index
    @uploads=Upload.all
  end

  def new
    @upload=Upload.new
  end


  def create

    @upload=Upload.new(params_abc)

     if  @upload.save  
        redirect_to  @upload
     else
       render 'new'
     end

  end
def show
    @post = find_params
  end

private

  def params_abc
        params.require(:upload).permit(:title,:description,:tageline)
  end
  def find_params
      Post.find(params[:id])
    end

end


[上传/ new.html.erb]

<%= form_for @upload,html: { multipart: true } do |f| %>
    <% if @upload.errors.any? %>
        <div id="errors">
            <h2><%= pluralize(@upload.errors.count, "error") %> prevented this post from saving:</h2>
            <ul>
                <% @upload.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
    <br>

    <%= f.label :description %><br>
    <%= f.text_field :description %><br>
     <br>

    <%= f.label :tageline %><br>
    <%= f.file_field :tageline%><br>
    <br>

    <%= f.submit %>
<% end %>


[上传/ index.html.erb]

<% @uploads.each do |f| %>                      

<div id="page_wrapper">

        <div id="profile_image">
            <%= f.image "uploadss/post/"%>
        </div>

        <div id="content_link">
            <div id="content">
                <p>Being the richest man in the cemetery doesn't matter to me. Going to bed at night saying we've done something wonderful, that's what matters to me.
                </p>        
            </div>

            <div id="link">

                <button type="button" >Read More</button>
            </div>  
        <div>

</div>
    <br>
<% end %>


[移民]

class CreateUploads < ActiveRecord::Migration
  def change
    create_table :uploads do |t|
        t.string :title, null: false
        t.string :description, null: false
        t.string :tageline, null: false
        t.timestamps
    end
  end
end


[upload.rb]

class Upload < ActiveRecord::Base

    validates :description,  presence: true
    validates :title,  presence: true
    validates :tageline, presence: true

    mount_uploader :tageline, TagelineUploader 


end


[载]

# encoding: utf-8

class TagelineUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploadss/post/#{model.id}"  
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

1 个答案:

答案 0 :(得分:2)

您应该在uploads / index.html.erb文件中执行以下操作:

 <%= image_tag(f.tageline.url) %>

int与显示其他字段的方式相同:

 <%= f.title %>
 <%= f.description %>