title: Rails 4 - Carrierwave -file uploads
body: <p>I'm trying to make an app in Rails 4.</p>
  
  <p>I use Carrierwave for file uploads.</p>
  
  <p>I have two models, user &amp; profile.</p>
  
  <p>They each have:</p>
  
  <p>profile.rb</p>
  
  <pre><code>mount_uploader :hero, HeroUploader
  </code></pre>
  
  <p>user.rb</p>
  
  <pre><code>  mount_uploader :avatar, AvatarUploader
  </code></pre>
  
  <p>I then have uploaders as:</p>
  
  <p>Hero:</p>
  
  <pre><code>class HeroUploader &lt; 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
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{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 :resize_to_fit =&gt; [950, 245]
    # process :scale =&gt; [200, 300]
    #
    # def scale(width, height)
    #   # do something
    # end
  
    # Create different versions of your uploaded files:
    version :wide do
      process :resize_to_fill =&gt; [951,245]
    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
  </code></pre>
  
  <p>Avatar:</p>
  
  <pre><code>class AvatarUploader &lt; 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
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  
    def cache_dir
      "#{Rails.root}/tmp/uploads"
    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 =&gt; [200, 300]
    #
    # def scale(width, height)
    #   # do something
    # end
  
    # Create different versions of your uploaded files:
    # version :thumb do
    #   process :resize_to_fit =&gt; [50, 50]
    # end
  
    process :resize_to_fit =&gt; [800, 800]
    # Create different versions of your uploaded files:
    version :thumb do
      process :resize_to_fill =&gt; [200,200]
    end
  
    version :profile do
      process :resize_to_fill =&gt; [345,245]
    end
  
    version :wide do
      process :resize_to_fill =&gt; [951,245]
    end
  
    version :small do
      process :resize_to_fill =&gt; [35,35]
    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
  </code></pre>
  
  <p>I'm still in development mode (so not sure if that makes a difference).</p>
  
  <p>In my profile form I have:</p>
  
  <pre><code>&lt;%= simple_form_for(@profile) do |f| %&gt;
              &lt;%= f.error_notification %&gt;
  
                &lt;div class="form-inputs"&gt;
  
            &lt;div class="intpol2"&gt;
              About you
            &lt;/div&gt;
  
            &lt;div class="row"&gt;
              &lt;div class="col-md-4"&gt;
                &lt;%= f.input :title,  autofocus: true %&gt;
              &lt;/div&gt;
  
              &lt;div class="col-md-8"&gt;
                &lt;%= f.input :occupation, :label =&gt; "Your occupation or job title" %&gt;
              &lt;/div&gt;
  
            &lt;/div&gt;
  
            &lt;div class="row"&gt;
              &lt;div class="col-md-6"&gt;
                 &lt;%= render 'users/profileimgform', f: f %&gt;   
              &lt;/div&gt;
  
              &lt;div class="col-md-6"&gt;
               &lt;%= f.input :hero, as: :file, :label =&gt; "Add a background image to your profile page" %&gt;
              &lt;/div&gt;
  
            &lt;/div&gt;
  </code></pre>
  
  <p>And my user partial form for the profile avatar image has:</p>
  
  <pre><code>&lt;%= simple_fields_for :user do |f| %&gt;
    &lt;%= f.error_notification %&gt;
  
                &lt;div class="form-inputs"&gt;
  
  
                &lt;%= f.input :avatar, as: :file, :label =&gt; "Add a profile image (head shot)" %&gt;
  
                &lt;/div&gt;
  
  
          &lt;% end %&gt;
  </code></pre>
  
  <p>When I save all this and try to use the form to upload images, I get two different results.</p>
  
  <p>For the user avatar image, I get nothing displayed in the show page. When I inspect the element, there is an empty div container showing.</p>
  
  <p>However,for the profile hero image, the show page shows a broken link image, and when I inspect the element, I get:</p>
  
  <pre><code>Failed to load resource: the server responded with a status of 404 (Not Found)
  </code></pre>
  
  <p>Is there another step to making this work?</p>
  
  <p>My profile controller has strong params which include both of these attributes:</p>
  
  <pre><code>def profile_params
        params[:profile].permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, 
          :working_languages, :tag_list,
            user_attributes: [:avatar]
            )
      end
  </code></pre>
  
  <p>Taking Alexei's suggestion below, I changed the opening line of the profiles form to:</p>
  
  <pre><code>&lt;%= simple_form_for @profile, html: { multipart: true }  do |f| %&gt;
  </code></pre>
  
  <p>When I save this and try again, I get exactly the same error as previously.</p>
  
  <p>In my profiles show page, I have:</p>
  
  <pre><code>&lt;%= image_tag '@profile.user.avatar.url.profile' if @profile.user.avatar? %&gt;
  
              &lt;%= image_tag '@profile.hero.url.wide' if @profile.hero? %&gt; %&gt;
  </code></pre>
  
  <p>These image tags are in the same show page. The avatar attribute is in the users table (profile belongs to user) and the hero attribute is in the profile table. The difference in errors is that the div tag containing the avatar just appears as blank in the code inspector. The div tag containing the hero image, appears as a broken link and shows a 404 error in the code inspector.</p>
  
  <p>In my articles show page I have the same problem, but instead of rendering the image, it shows the typed name of the image file.</p>