Jack Gruber Jack Gruber - 12 days ago 10
Ruby Question

Rails 5 CarrierWave error "nil is not a valid asset source"

Im getting an error "nil is not a valid asset source" when I got to the show page in my app. I'm not sure If it the image is just not saving or how I am attempting to display it is wrong.

Uploader folder:

class AvatarUploader < CarrierWave::Uploader::Base

storage :file

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


Profile Model:

class Profile < ApplicationRecord

has_many :albums

mount_uploader :image, AvatarUploader
end


Schema:

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


The Field from the View where the images gets uploaded:

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


The Form View That I fill out and add the Picture and Info:

<%= 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 %>






And the Show view where I get the error:

<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 %>


I submit the image and get the error message with the line from the show view above highlighted.

"Showing /home/ubuntu/workspace/music-db/app/views/profiles/show.html.erb where line #5 raised:

nil is not a valid asset source"

From reading other similar issues. It us maybe that the image is not saving correctly. As if there was image save it would not be nil. But really I am just not sure. Am only trying out this gem for the first time.

Any and all help is greatly appreciated.

Answer

Change your method to accpet image param well

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

to

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

As well check you should install gem 'mini_magick'