2016-02-08 86 views
-2

我正在嘗試構建一個rails應用程序,使用carrierwave上傳圖像。上傳後我想要檢索該圖像並顯示在我的索引頁中,但我不知道如何指定圖像的路徑或如何檢索圖像,並顯示在view.Also我要像顯示標題和描述的細節我已上傳
[uploads_controller.rb]
Rails獲取圖像並顯示在我的索引視圖

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 


[uploads/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 

回答

2

你應該做這樣的事情在你的上傳/ index.html.erb文件:

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

int可以用同樣的方法顯示其他字段:

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

thx它正在工作 –

相關問題