I
I
ivasenkoartem2018-05-26 23:44:53
Ruby on Rails
ivasenkoartem, 2018-05-26 23:44:53

When loading an image via CarrierWave, the page with the form is updated. How to fix it?

I'm uploading an image via a form, but the page refreshes when submitting.. The image ends up in a folder, but doesn't show up on output. How to fix? Here is the code:

projects_controller.rb
class ProjectsController < ApplicationController

  before_action :set_project, only: [ :show, :edit, :update, :destroy]

  def index
    @projects = Project.all
  end

  def show
  end

  def new
    @project = Project.new
  end

  def create
    @project = Project.new(project_params)
    if @project.save
      redirect_to @project
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @project.update_attributes(project_params)
      redirect_to @project
    else
      render :edit
    end	
  end

  def destroy
    @project.destroy
    redirect_to root
  end


  private 

  def set_project
    @project = Project.find(params[:id])
  end

  def project_params
    params.require(:project).permit(:name, :title, :content, :work, :image)
  end
end
project.rb
class Project < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end
image_uploader.rb
class ImageUploader < 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(*args)
  #   # 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 :large do
    process resize_to_fit: [100000, 100000]
  end

  version :thumb do
    process resize_to_fit: [300, 700]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %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
views
<div class="col-md-4 <%= project.type %>">
    <a href="projects/<%= project.id %>" class="portfolio_item work-grid wow fadeInUp">
      <%= image_tag project.image.url(:thumb) %>
      <div class="portfolio_item_hover">
        <div class="item_info">
          <span><%= project.name %></span>
          <em><%= project.work %></em>
        </div>
      </div>
    </a>
  </div>
form
<%= form_for @project, html: { multipart: true } do |f| %>
  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_area :title %>
  </div>
  <div class="form-group">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>
  <div class="form-group">
    <%= f.label :work %>
    <%= f.text_field :work %>
  </div>
  <div class="form-group">
    <%= f.file_field :image %>
  </div>
  <div class="form-group">
    <%= f.submit 'Сохранить' %>
    
  </div>
<% end %>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question