title: Rails4 : How do I display and edit uploaded file using carrierwave?
body: <p>I can upload a file and save the file name in the database.
  But the file name doesn't appear when I edit.</p>
  
  <p>I'd like to:</p>
  
  <ol>
  <li>display the file name and the uploaded image when click "Edit" link in _article.html.erb.</li>
  <li>display the uploaded image in _article.html.erb.</li>
  </ol>
  
  <p>article.rb</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
      .
      .
      has_many :photos, dependent: :destroy
      accepts_nested_attributes_for :photos
      .
      .
  end
  </code></pre>
  
  <p>photo.rb</p>
  
  <pre><code>class Photo &lt; ActiveRecord::Base
      belongs_to :article
      mount_uploader :image, ImageUploader
      validates :image, presence: true
      #validates :article_id, presence: true
  end
  </code></pre>
  
  <p>.schema articles</p>
  
  <pre><code>CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
  "content" varchar(255), 
  "user_id" integer, 
  "created_at" datetime, 
  "updated_at" datetime,);
  </code></pre>
  
  <p>.schema photos</p>
  
  <pre><code>CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
  "article_id" integer, 
  "image" varchar(255), 
  "created_at" datetime, 
  "updated_at" datetime);
  </code></pre>
  
  <p>articles_controller.rb</p>
  
  <pre><code>class ArticlesController &lt; ApplicationController
      .
      .
    def new
      @article = Article.new
      @article.photos.build
      .
      .
    end
  
    def create
      @article = current_user.articles.build(article_params)
      .
      .
    end
  
    def edit
      @article = Article.find(params[:id])
    end
  
    def update
      @article = Article.find(params[:id])
      if @article.update(article_params)
        redirect_to current_user
      else
        render 'edit'
      end
    end
      .
      .
    private
  
      def article_params
        params.require(:article).permit(:content, photos_attributes: [:id, :article_id, :image])
      end
      .
      .
  end
  </code></pre>
  
  <p>articles\ _article.html.erb</p>
  
  <p><strong>I'd like to display the uploaded image and the file name here and after clicking "Edit" link (_article_form.html.erb)</strong></p>
  
  <pre><code>&lt;li&gt;
      .
      .
    &lt;span class="content"&gt;&lt;%= article.content %&gt;&lt;/span&gt;
    &lt;% if current_user?(article.user) %&gt;
      &lt;%= link_to "Edit", edit_article_path(article.id), class: "btn btn-default" %&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>articles\edit.html.erb</p>
  
  <pre><code>&lt;h1&gt;Update article&lt;/h1&gt;
  &lt;div class="row"&gt;
          &lt;%= render 'shared/article_form' %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>shared\ _article_form.html.erb</p>
  
  <p><strong>When I edit, ":content" is displayed. But ":image" is "no file selected" ...</strong></p>
  
  <pre><code>&lt;%= form_for(@article) do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content, placeholder: "Compose new article..." %&gt;
      &lt;%= f.fields_for :photos do |p| %&gt;
        &lt;%= p.hidden_field :article_id %&gt;
        &lt;%= p.label :image %&gt;
        &lt;%= p.file_field :image %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
    &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>development.log (when I submit new article)</p>
  
  <pre><code>Started POST "/articles" for 127.0.0.1 at 2014-07-05 16:31:11 +0900
  Processing by ArticlesController#create as HTML
    Parameters: {"utf8"=&gt;"?","authenticity_token"=&gt;"uaWcqBZ6rhS/NIal/...=", "article"=&gt;{"category_id"=&gt;"6", "content"=&gt;"last", "photos_attributes"=&gt;{"0"=&gt;{"article_id"=&gt;"", "image"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x3cb0910 @tempfile=#&lt;File:C:/.../AppData/Local/Temp/RackMultipart20140705-6112-1q9t7r6&gt;, @original_filename="DSCN0721_080.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[photos_attributes][0][image]\"; filename=\"DSCN0721_080.JPG\"\r\nContent-Type: image/jpeg\r\n"&gt;}}}, "commit"=&gt;"Post"}
    [1m[35mUser Load (1.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '...' LIMIT 1
    [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
    [1m[35mSQL (3.0ms)[0m  INSERT INTO "articles" ("content", "created_at", "category_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "last"], ["created_at", Sat, 05 Jul 2014 07:31:11 UTC +00:00], ["category_id", 6], ["updated_at", Sat, 05 Jul 2014 07:31:11 UTC +00:00], ["user_id", 1]]
    [1m[36mSQL (27.0ms)[0m  [1mINSERT INTO "photos" ("article_id", "created_at", "image", "updated_at") VALUES (?, ?, ?, ?)[0m  [["article_id", 306], ["created_at", Sat, 05 Jul 2014 07:31:11 UTC +00:00], ["image", "DSCN0721_080.JPG"], ["updated_at", Sat, 05 Jul 2014 07:31:11 UTC +00:00]]
    [1m[35m (5.0ms)[0m  commit transaction
  Redirected to http://localhost:3000/users/1
  Completed 302 Found in 128ms (ActiveRecord: 36.0ms)
  </code></pre>