Carrierwave宝石不起作用

时间:2017-05-06 22:21:45

标签: ruby-on-rails ruby carrierwave

我真的不知道现在该做什么。图像在数据库中显示为nil。没有出现错误。

模型

class Resource < ActiveRecord::Base
mount_uploader :image, ImageUploader
attr_accessible :title, :url, :description, :tag
validates :title, presence: true, uniqueness: true
validates :url, presence: true
validates :description, presence: true
validates :tag, presence: true
belongs_to :user
acts_as_votable
validates_processing_of :image
validate :image_size_validation
  private
  def image_size_validation
    errors[:image] << "should be less than 500KB" if image.size > 0.5.megabytes
  end
  end

图片上传器

class ImageUploader < CarrierWave::Uploader::Base

 include CarrierWave::MiniMagick

 storage :file

 def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end

 version :thumb do
   process :resize_to_fill => [100, 100]
 end

 def extension_white_list
   %w(jpg jpeg gif png)
 end
 end

表格部分

<%= form_for @resource, html: { multipart: true } do |f| %>
  <% if @resource.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@resource.errors.count, "error") %> prohibited this pet 
    from being saved:</h2>

    <ul>
     <% @resouce.errors.full_messages.each do |message| %>
    <li><%= message %></li>
   <% end %>
  </ul>
</div>
  
<div class="field">
  <%= f.label :title %><br>
  <%= f.text_field :title %>
</div>

<div class="field">
  <%= f.label :url %><br>
  <%= f.text_area :url%>
</div>

<div class="field">
 <%= f.label :description %><br>
 <%= f.text_area :description %>
</div>

<div class="field">
 <%= f.label :tag %><br>
 <%= f.text_area :tag %>

<div class="field">
 <%= f.label :image %><br>
 <%= f.file_field :image %>
 <% if f.object.image? %>
   <%= image_tag f.object.image.thumb.url %>
   <%= f.label :remove_image %>
   <%= f.check_box :remove_image %> 
<% end %>

<div class="actions">
 <%= f.submit %>
</div>
   

show

中的图片标记
<%= image_tag @resource.image.thumb.url %>

控制器

class ResourcesController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]

 def index
  @resources = Resource.page(params[:page]).per(15)
 end

 def show
    @resource = Resource.where(:id => params[:id]).first
 end

 def edit
  @resource = Resource.where(:id => params[:id]).first
 end

 def new
  @resource = current_user.resources.build
 end

 def update
  @resource = Resource.where(:id => params[:id]).first

  respond_to do |format|
   if @resource.update_attributes(resources_params)
    format.html { redirect_to @resource, :notice => 'Resource was successfully 
    updated.' }
    format.json { head :no_content }
   else
    format.html { render action: "edit" }
    format.json { render json: @resource.errors, :status => 
   :unprocessable_entity }
  end
 end
end


def destroy
  @toDelete = Resource.where(:id => params[:id]).first
  puts @toDelete.id

 if @toDelete.destroy
  flash[:notice] = "Successfully deleted post!"
  redirect_to root_path
 else
  flash[:alert] = "Error updating post!"
 end
end


 def create
   @resource= current_user.resources.build(resources_params)
   if @resource.save
    redirect_to root_path
    flash[:alert] = "Successfully created new resource!"
   else
    render "new"
   end

 end


 def upvote
  @resource = Resource.find(params[:id])
  @resource.upvote_by current_user
  redirect_to :back
 end

  def downvote
   @resource = Resource.find(params[:id])
   @resource.downvote_from current_user
   redirect_to :back
  end


  private

 def resources_params
    params.require(:resource).permit(:title, :url, :description, :image, :tag)
 end

end

如果您想查看其他任何文件,我会发布。

谢谢。

修改

这只是一个版本问题......

0 个答案:

没有答案