【问题标题】:NoMethodError (undefined method `upload' for nil:NilClass):NoMethodError(nil:NilClass 的未定义方法“上传”):
【发布时间】:2021-08-20 06:22:08
【问题描述】:

解决这个问题对我来说是一场噩梦。 NoMethodError (undefined method `upload' for nil:NilClass): 请帮我摆脱它。 我正在使用导轨 5.2.6。我已经为文件上传(图片和视频)安装了活动存储。

class Medium < ApplicationRecord
enum mediatype: [:image, :video, :text]

has_one_attached :file

validates :mediatype, :mediatitle, presence: true
end

这是我的控制器

class MediaController < ApplicationController
    before_action :authenticate_user!  


    def index
        @media = Medium.all.group_by(&:mediatype)
        @medium = Medium.new
    end

    def create
        @medium = Medium.create(medium_params)
        if @medium.save
           redirect_to media_path, notice: "Medium Created Successfully"    
        else  
            render :index, alert:"Unable to create new"
        end  
    end

    private

    def medium_params
        params.require(:medium).permit(:mediatitle,:description,:mediatype, :file, 
     :text)
    end

end

这是我的看法

  - @media.each.with_index do |(mediatype,data),index|
.tab-pane.fade.show.rounded class=(index == 0 ? "active":"") id="#{mediatype}" aria-labelledby="#{mediatype}-tab" role="tabpanel"
   table.table.table-hover.px-5
     thead.text-center.text-white.bg-dark 
       tr
         th.space S.N 
         th Media Title 
         th Description 
         th Preview
         th Action
     tbody.text-center.px-5
       - data.each.with_index(1).each do |data,index|
         tr 
           td = index
           td = data.mediatitle
           td = data.description
           td 
             - if data.image? || data.video?
                 -if data.image?
                    = image_tag data.file if data.file.attached? 
                 -else data.video?
                    = video_tag (url_for(data.file)) if data.file.attached? 
             - else data.text?
              = data.text 
           td 
              = link_to "", edit_medium_path(data), class: "fa fa-edit text-success fa-lg mr-2",'data-toggle': 'tooltip', 'data-placement':'left', title: 'Edit media' 
              = link_to "",medium_path(data), method: :delete, data: { confirm: "Are you Sure??" }, class: "fa fa-trash fa-lg text-danger",'data-toggle': 'tooltip', 'data-placement':'left', title: 'delete media'

当我运行服务器/rails c/rails db:migrate

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test 
  environment)
  * production - set it to true

我已经检查了所有文件,但它是正确的,如下所述。 当我上传图片或视频时出现错误:

NoMethodError (undefined method `upload' for nil:NilClass):

我没有改变我的开发,但显示了像生产这样的错误。 error shown as a production

我已经删除了我的数据库并再次创建,清除缓存,迁移它,谷歌所有可能的解决方案,但它们都不起作用

拜托,请帮帮我!!

【问题讨论】:

  • 也许你可以在开发中运行它并使用调试器来识别哪个变量是nil
  • 我正在开发中。我还没有将我的项目更改为生产环境

标签: ruby-on-rails


【解决方案1】:

您的环境似乎设置不正确。尝试明确设置:

RAILS_ENV=development bundle exec rails db:migrate

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2013-01-14
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多