【问题标题】:Rails 5 CarrierWave error "nil is not a valid asset source"Rails 5 CarrierWave 错误“nil 不是有效的资产来源”
【发布时间】:2017-04-10 07:05:18
【问题描述】:

当我进入应用程序的显示页面时,我收到错误消息“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 行的位置:

nil 不是有效的资产来源"

来自阅读其他类似问题。我们可能是图像没有正确保存。好像有图像保存它不会是零。但实际上我只是不确定。我只是第一次尝试这个宝石。

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

【问题讨论】:

  • 是的,您的图像未存储..您可以粘贴整个视图表单和控制器方法吗?
  • 我已经添加了完整的视图表单和显示以及控制器。希望这足以说明问题。添加“除非@profile.image.blank?”正如下面 Askanksha 所建议的那样,确实会显示默认图片,但它并不能帮助我实际存储和显示我想要显示的图像。
  • 你安装 gem 'mini_magick' 了吗??

标签: ruby-on-rails ruby file-upload carrierwave


【解决方案1】:

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

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

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

我还没有检查过,但两者中的任何一个都适合你。

【讨论】:

  • 嘿,这确实有助于我在图片不显示时显示默认图片。但我认为问题是图像没有为我保存,所以每次都默认显示。谢谢你的额外一点。它很有用,我会将它添加到我的代码中。但它并没有完全解决我的图像不保存的问题。
【解决方案2】:

改变你的方法来很好地接受图像参数

 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'

【讨论】:

  • 哈哈,就在你发这个之前,我发现了错误。感谢您的帮助。非常感谢。
【解决方案3】:

在添加所有额外代码时,我只是注意到在我的私人配置文件参数中我从未添加过图像。因此,由于强大的参数,它不允许我保存图片。如果这有意义的话。

感谢两位的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2012-05-08
    • 2020-01-05
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多