Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带Carrierwave的不允许参数_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 带Carrierwave的不允许参数

Ruby on rails 带Carrierwave的不允许参数,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,当我试图用Rails和Carrierwave上传一个文件时,我总是遇到一个params错误。没有关联,没有多态性,只是一个简单的附件模型,不属于任何东西: 控制器: class AttachmentsController < ApplicationController def create @attachment = Attachment.new(attachment_params) if @attachment.save flash.now[:notice] =

当我试图用Rails和Carrierwave上传一个文件时,我总是遇到一个params错误。没有关联,没有多态性,只是一个简单的附件模型,不属于任何东西:

控制器:

class AttachmentsController < ApplicationController

def create
  @attachment = Attachment.new(attachment_params)
    if @attachment.save
      flash.now[:notice] = 'Done!'
      redirect_to root_path
    else
      flash.now[:error] = 'Error.'
      redirect_to root_path
    end
end

private

    def attachment_params
      params.require(:attachment).permit(:file)
    end
class AttachmentsController
日志:

2014-11-06 15:11:48+0300时开始发布127.0.0.1的“
附件”
由AttachmentsController处理#创建为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”b3juochivc4m1srveiznfaxbsenmdah1c1hrhcse4=“,“附件”=>{“附件”=>,“提交”=>“上传我的列表”}
不允许的参数:附件
(0.6ms)开始
SQL(0.7ms)插入“附件”(“创建时间”,“更新时间”)值($1,$2),返回“id”[[“创建时间”,“2014-11-06 12:11:48.966191”],[“更新时间”,“2014-11-06 12:11:48.966191”]]
(0.4ms)提交
重定向到http://localhost:3000/
在29毫秒内完成302次(ActiveRecord:1.8毫秒)
使用型号更新了

class Attachment < ActiveRecord::Base
  mount_uploader :file, FileUploader
end
类附件
上载程序:

class FileUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(pdf xls xlsx csv txt doc docx)
  end

end
class FileUploader
只需更改

params.require(:attachment).permit(:file)

因为您会收到参数:
“attachment”=>{“attachment”=>#只需更改即可

params.require(:attachment).permit(:file)

因为您会收到参数:
“attachment”=>{“attachment”=>#只需更改即可

params.require(:attachment).permit(:file)

因为您会收到参数:
“attachment”=>{“attachment”=>#只需更改即可

params.require(:attachment).permit(:file)


因为您收到参数:
“attachment”=>{“attachment”=>#我尝试了它,它抛出:“unknown attribute:attachment”,日志显示:Started POST“/attachments”,以便…由AttachmentsController处理#创建为HTML参数:{“utf8”=>”✓", "真实性令牌“=>”…,“附件”=>{“附件”=>“上传我的列表”}在100ms ActiveRecord中完成了500个内部服务器错误::UnknownAttributeError-未知属性:attachments您可以将模型和视图添加到问题中吗?模型看起来很好,视图呢?是的,非常感谢,您对视图的看法是对的。我尝试了它,它抛出:“未知属性:附件”,日志显示:Started POST”/attachments对于…由AttachmentsController处理#创建为HTML参数:{“utf8”=>“✓", "真实性令牌“=>”…,“附件”=>{“附件”=>“上传我的列表”}在100ms ActiveRecord中完成了500个内部服务器错误::UnknownAttributeError-未知属性:attachments您可以将模型和视图添加到问题中吗?模型看起来很好,视图呢?是的,非常感谢,您对视图的看法是对的。我尝试了它,它抛出:“未知属性:附件”,日志显示:Started POST”/attachments对于…由AttachmentsController处理#创建为HTML参数:{“utf8”=>“✓", "真实性令牌“=>”…,“附件”=>{“附件”=>“上传我的列表”}在100ms ActiveRecord中完成了500个内部服务器错误::UnknownAttributeError-未知属性:attachments您可以将模型和视图添加到问题中吗?模型看起来很好,视图呢?是的,非常感谢,您对视图的看法是对的。我尝试了它,它抛出:“未知属性:附件”,日志显示:Started POST”/attachments对于…由AttachmentsController处理#创建为HTML参数:{“utf8”=>“✓", "Authority_token“=>”…,“attachment”=>{“attachment”=>#“上载我的列表”}在100ms ActiveRecord中完成了500个内部服务器错误::UnknownAttributeError-unknown属性:Attachments您可以将模型和视图添加到问题中吗?模型看起来很好,视图?是的,非常感谢,您的视图是正确的
<%= f.file_field :file %>