Rails 5 CarrierWave 错误 "nil is not a valid asset source"
Rails 5 CarrierWave error "nil is not a valid asset source"
当我进入我的应用程序中的显示页面时,我收到错误消息 "nil is not a valid asset source"。我不确定是不是图片没有保存,还是我试图显示它的方式有误。
上传文件夹:
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 行出现的位置:
nil 不是有效的资产来源
来自阅读其他类似问题。可能是图像没有正确保存。好像有图像保存它不会是零。但我真的不确定。我只是第一次尝试这个 gem。
非常感谢任何帮助。
这意味着您的图像字段现在是空白的。换句话说,它是 nil 并且没有存储在其中的字段名称。为防止错误,请先检查图像是否存在。
<p>
<%= image_tag @profile.image_url if @profile.image.present? %>
</p>
或
<p>
<%= image_tag @profile.image_url unless @profile.image.blank? %>
</p>
我还没有检查过,但两者中的任何一个都适合你。
更改您的方法以很好地接受图像参数
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'
在添加所有额外代码时,我只注意到在我的私人配置文件参数中我从未添加图像。因此,由于强大的参数,它不允许我保存图片。如果说得通。
感谢两位的帮助。
当我进入我的应用程序中的显示页面时,我收到错误消息 "nil is not a valid asset source"。我不确定是不是图片没有保存,还是我试图显示它的方式有误。
上传文件夹:
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 行出现的位置:
nil 不是有效的资产来源
来自阅读其他类似问题。可能是图像没有正确保存。好像有图像保存它不会是零。但我真的不确定。我只是第一次尝试这个 gem。
非常感谢任何帮助。
这意味着您的图像字段现在是空白的。换句话说,它是 nil 并且没有存储在其中的字段名称。为防止错误,请先检查图像是否存在。
<p>
<%= image_tag @profile.image_url if @profile.image.present? %>
</p>
或
<p>
<%= image_tag @profile.image_url unless @profile.image.blank? %>
</p>
我还没有检查过,但两者中的任何一个都适合你。
更改您的方法以很好地接受图像参数
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'
在添加所有额外代码时,我只注意到在我的私人配置文件参数中我从未添加图像。因此,由于强大的参数,它不允许我保存图片。如果说得通。
感谢两位的帮助。