gpt4 book ai didi

ruby-on-rails - 为什么当对象不为零时,事件存储会抛出此错误 : undefined method `upload' for nil:NilClass,

转载 作者:行者123 更新时间:2023-12-04 19:27:24 25 4
gpt4 key购买 nike

我已经在 Rails 项目上设置了事件存储。我不断收到错误:undefined method 'upload' for nil:NilClass .

这是我所做的。

  • 我已经运行了迁移:rails active_storage:install
  • 我编辑了我的开发环境文件:config.active_storage.service = :local
  • 与我的生产文件相同:config.active_storage.service = :amazon
  • 我确保我的模型设置正确 has_one_attached :image

  • 我的 Controller :
    class EventsController < ApplicationController 
    def create
    @event = Event.new(event_params)
    @event.save
    end

    private

    def event_params
    params.require(:event).permit(:name, :image)
    end
    end

    这是表格:
    <%= simple_form_for(@event, url: events_path, method: :post) do |f| %>
    <%= f.input :name %>

    <%= f.input :image %>

    <%= f.submit %>
    <% end %>

    这是模型:
    class Event < ActiveRecord::Base

    has_one_attached :image

    acts_as_list

    end

    值得一提的是,我正在将这个特定项目升级到 rails 5。它也曾经使用过回形针。

    问题:
    当我提交此表单时,会引发上述错误。相同
    尝试更新 @event 对象时发生错误。

    更新
  • 查看框架跟踪时,在这一行抛出错误:blob.upload io .奇怪的是,当我调试这条线时,blob不是零。它拥有一个 ActiveStorage::Blob它没有 ID。和 io也不是零。

  • 错误日志:

    我特意取出了真品 token 。
    Started POST "/events" for ::1 at 2018-08-23 10:02:25 -0600
    Processing by EventsController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"*****************", "event"=>{"name"=>"test event", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007fc816e90af8 @tempfile=#<Tempfile:/var/folders/f9/q3x5477d1dxd7fxjbrl2tvch0000gn/T/RackMultipart20180823-61111-14dih2d.jpg>, @original_filename="sample-image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image]\"; filename=\"sample-image.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update Event"}
    Completed 500 Internal Server Error in 61ms (ActiveRecord: 0.0ms)



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

    app/controllers/events_controller.rb:4:in `create'
    ::1 - - [23/Aug/2018:10:02:25 MDT] "POST /events HTTP/1.1" 500 138644
    http://localhost:3000/ -> /events

    最佳答案

    您应该将这行代码添加到您的 Gemfile 中:

    gem 'aws-sdk-s3', require: false

    然后将此其他添加到您的环境/生产中:
    config.active_storage.service = :amazon # or wharever storage you are using...

    关于ruby-on-rails - 为什么当对象不为零时,事件存储会抛出此错误 : undefined method `upload' for nil:NilClass,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989783/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com