2016-11-25 49 views
1

当我到达应用程序中的显示页面时,出现错误“nil不是有效的资产源”。我不确定如果图像只是不保存或我试图显示它是错误的。Rails 5 CarrierWave错误“nil不是有效的资产源”

上传文件夹:

class AvatarUploader < CarrierWave::Uploader::Base 

storage :file 

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

简介型号:

class Profile < ApplicationRecord 

    has_many :albums 

    mount_uploader :image, AvatarUploader 
end 

模式:

create_table "profiles", force: :cascade do |t| 
    t.string "name" 
    t.date  "born" 
    t.string "bio" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "image" 
end 

从视场,其中图片被上传:

class ProfilesController < ApplicationController 
before_action :set_profile, only: [:show, :edit, :update, :destroy] 

def index 
    @search = Profile.search(params[:q]) 
    @profiles = @search.result(distinct: true) 
end 

def show 
end 

def new 
    @profile = Profile.new 
end 

def edit 
end 

def create 
    @profile = Profile.new(profile_params) 

    respond_to do |format| 
    if @profile.save format.html { redirect_to @profile, notice: 'Profile was successfully  created.' } 
    format.json { render :show, status: :created, location: @profile } 
    else 
    format.html { render :new } 
    format.json { render json: @profile.errors, status: :unprocessable_entity } 
    end 
    end 
end 

def update 
respond_to do |format| 
    if @profile.update(profile_params) 
    format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } 
    format.json { render :show, status: :ok, location: @profile } 
    else 
    format.html { render :edit } 
    format.json { render json: @profile.errors, status: :unprocessable_entity } 
    end 
    end 
    end 

def destroy 
    @profile.destroy 
respond_to do |format| 
    format.html { redirect_to profile_url, notice: 'Profile was successfully destroyed.' } 
    format.json { head :no_content } 
    end 
end 

private 

def set_profile 
    @profile = Profile.find(params[:id]) 
end 

def profile_params 
    params.require(:profile).permit(:name, :bio, :born) 
end  
end 

的形式查看我填写,并添加图片和信息:

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

<div class="field"> 
<%= f.file_field :image %><br> 
</div> 

<div class="field"> 
<%= f.label :name %><br> 
<%= f.text_field :name %> 
</div> 
<br> 
<div class="field"> 
<%= f.label :bio %><br> 
<%= f.text_area :bio %> 
</div> 
<br> 
<div class="field"> 
<%= f.label :born %><br> 
<%= f.date_select :born %> 
</div> 
<br> 
<div class="actions"> 
<%= f.submit %> 

和展视在那里我得到的错误:

<p> 
<%= image_tag @profile.image_url %> 
</p> 

<p> 
<strong>Name:</strong> 
<%= @profile.name %> 
</p> 

<p> 
<strong>Bio:</strong> 
<%= @profile.bio %> 
</p> 

<p> 
<strong>Born:</strong> 
<%= @profile.born %> 
</p> 

<%= link_to 'Edit', edit_profile_path(@profile) %> | 
<%= link_to 'Back', profiles_path %> 

我提交图像并从上面突出显示的视图中获得错误信息。

“显示/home/ubuntu/workspace/music-db/app/views/profiles/show.html.erb其中线#5提出:

零是不是一个有效资源源”

从阅读其他类似的问题。我们可能认为图像没有正确保存。就好像有图像保存它不会是零。但我真的不确定。我只是第一次尝试这个宝石。

任何和所有的帮助,非常感谢。

+0

是的,你的形象不存储..你可以粘贴整个视图窗体和控制器的方法? –

+0

我已添加完整视图窗体并显示控制器。希望这足以说明问题。添加“除非@ profile.image.blank?”正如下面的Askanksha所暗示的那样,确实会使默认图片显示出来,但它并不能帮助我实际存储并显示想要显示的图像。 –

+0

你安装了gem'mini_magick'吗? –

回答

3

更改方法Accpet头图像PARAM以及

def profile_params 
    params.require(:profile).permit(:name, :bio, :born) 
end 

def profile_params 
    params.require(:profile).permit(:name, :bio, :born, :image) 
end 

除了检查应该安装gem 'mini_magick'

+0

哈哈,就在你发布这个之前,我发现了这个错误。感谢WishZone帮助。非常感激。 –

+0

谢谢@JackGruber,保持良好的工作:) –

0

这意味着您的图像字段现在是空白的。换句话说,它是零并且没有存储在其中的字段的名称。为防止出现错误,请检查图像是否首先出现。

<p> 
    <%= image_tag @profile.image_url if @profile.image.present? %> 
</p> 

<p> 
    <%= image_tag @profile.image_url unless @profile.image.blank? %> 
</p> 

我没有检查,但二者中的任何应为你工作。

+0

嘿,这确实帮助我显示图片不会显示时的默认图像。但我认为问题是图像不适合我保存,所以每次都默认显示。非常感谢你的额外一点。这很有用,我会将它添加到我的代码中。但它并没有完全解决我没有保存图像的问题。 –

0

并不是在所有的额外的代码添加我刚刚注意到,在我的私人档案params我从来没有添加图像。因为强大的参数,它不让我保存图片。如果这是有道理的。

感谢你们两位的帮助。

相关问题