2016-11-25 46 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我從來沒有添加圖像。因爲強大的參數,它不讓我保存圖片。如果這是有道理的。

感謝你們兩位的幫助。

相關問題