Задать вопрос
@ivasenkoartem

При загрузке изображения через CarrierWave обновляется страница с формой.. Как пофиксить?

Я загружаю изображение через форму, но при отправке страница обновляется.. Изображение попадает в папку, но не отображается при выводе. Как пофиксить? Вот код:

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 %>
  • Вопрос задан
  • 23 просмотра
Подписаться 1 Простой 1 комментарий
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы