Im, получая сообщение об ошибке «nil не является допустимым источником ресурсов», когда я попал на страницу показа в своем приложении. Я не уверен. Если это изображение просто не сохраняется или как я пытаюсь показать, это неправильно.Rails 5 Ошибка CarrierWave «nil не является допустимым источником активов»
Uploader Папка:
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 поднятый:
ноль не является действительным источником активов"
От чтения других подобных вопросов. Возможно, что изображение не сохраняется правильно. Как будто было спасение изображения, это было бы не ноль. Но на самом деле я просто не уверен. Я только впервые попробовал этот драгоценный камень.
Любая помощь очень ценится.
Да, ваше изображение не сохраняется. Вы можете вставить всю форму представления и метод контроллера? –
Я добавил полную форму представления и покажу плюс контроллер. Надеюсь, этого достаточно, чтобы иметь смысл. Добавление "if @ profile.image.blank?" как показывает Аскенша ниже, появляется изображение по умолчанию, но это не помогает мне на самом деле хранить и отображать изображение, которое я хочу показать. –
Вы установили gem 'mini_magick' ?? –