gpt4 book ai didi

ruby-on-rails - "undefined method ` 模型名称 ' for NilClass:Class"与 CarrierWave

转载 作者:行者123 更新时间:2023-11-30 23:47:30 25 4
gpt4 key购买 nike

现在我正在使用 CarrierWave 将照片上传到我的网站。问题是,当我打开照片/上传 (upload.html.erb) 时会显示此错误:

undefined method `model_name' for NilClass:Class



在我的 upload.html.erb 的第 1 行附近:
<%= form_for @photos, :html => {:multipart => true} do |f| %>  
<%= f.error_messages %>
<%= f.hidden_field :gallery_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :photo %>
</p>
<p><%= f.submit %></p>
<% end %>

我不太明白这一点,因为我认为我的模型 (photos.rb) 没有错误:
class Photos < ActiveRecord::Base
attr_accessor :gallery_id, :name, :photo
belongs_to :gallery
mount_uploader :photos, PhotosUploader
end

在我的 photos_controller.rb 我有这个:
class PhotosController < ApplicationController
def new
@photos = Photos.new(:gallery_id => params[:gallery_id])
end

def create
@photos = Photos.new(params[:photos])
if @photos.save
flash[:notice] = "Successfully created Photos."
else
render :action => 'new'
end
end

def edit
@photos = Photos.find(params[:id])
end
def update
@photos = Photos.find(params[:id])
if @photos.update_attributes(params[:photos])
flash[:notice] = "Successfully updated Photos."
else
render :action => 'edit'
end
end

def destroy
@photos = Photos.find(params[:id])
@photos.destroy
flash[:notice] = "Successfully destroyed Photos."
end
end

这是我的 photos_uploader.rb:
class PhotosUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :scale => [180, 180]
end
end

问候,伊万

最佳答案

问题可能与您已将上传器安装为 :photos 的事实有关。 ,但表单域是 :photo .尝试进行这些匹配。

关于ruby-on-rails - "undefined method ` 模型名称 ' for NilClass:Class"与 CarrierWave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6455595/

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