无法将数组投射到 - 带有载波的多个图像
can't cast Array to - Multiple images with carrierwave
我正在尝试使用载波上传多张图片,但在尝试保存时出现此错误
can't cast Array to
告诉我错误在控制器中
if @property.save
我不知道它是否相关,但是当我添加此迁移时
rails g migration add_images_to_properties images:json
模式为属性提出了这个问题table
# Could not dump table "properties" because of following NoMethodError
nil:NilClass
的未定义方法“[]”
一切正常..
class Property < ActiveRecord::Base
mount_uploaders :images, ImageUploader
end
def property_params
params.require(:property).permit(:address, :price, :city, {images: []})
end
<div class="property-image">
<h2>Cargar Imagenes</h2>
<div class="field">
<%= f.file_field :images, multiple: true %>
</div>
</div>
有线索吗??
在您的迁移过程中,您尝试创建一个 "images" 列并将 json 作为数据类型。
正如您的 schema.rb 告诉您的那样,您使用的数据库管理器无法做到这一点。
我假设您使用的是 Sqlite3,因为它是 rails 上的默认 dbm。
事实上,Sqlite 无法处理 json 数据类型
一个好的解决方案是将您的数据库从 Sqlite 迁移到 PostgreSql,因为它支持 json 并且它是 heroku 上的默认数据库管理器。
如果您在本地计算机上,要从 Sqlite3 迁移到 PG:
1 - 在本地机器上安装 Postgresql。根据您的 OS,您可以在这里找到您需要的内容:https://www.postgresql.org/download/
2- 更改您的 gem 文件 :
删除:
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
添加:
# Use postgres as the database for Active Record
gem 'pg'
3 - 运行 在你的命令行上
$ bundle install
它将在您的 Rails 应用程序上安装 postgresql gem
4 - 更新您的 database.yml 文件以使其适用于您的新数据库
从
更改您以前的文件
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
对此:
#
# gem install pg
#
# Ensure the PostgreSql gem is defined in your Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
password: the_password_you_set_when_installing_postgresql_on_your_machine
username: postgres
host: localhost
development:
<<: *default
database: db/development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test
5 - 现在您只需从命令行进行迁移:
首先重置数据库
$ rake db:setup
然后运行迁移
$ rake db:migrate
6 - 最后重启你的服务器
$ rails server
我正在尝试使用载波上传多张图片,但在尝试保存时出现此错误
can't cast Array to
告诉我错误在控制器中
if @property.save
我不知道它是否相关,但是当我添加此迁移时
rails g migration add_images_to_properties images:json
模式为属性提出了这个问题table
# Could not dump table "properties" because of following NoMethodError
nil:NilClass
的未定义方法“[]”一切正常..
class Property < ActiveRecord::Base
mount_uploaders :images, ImageUploader
end
def property_params
params.require(:property).permit(:address, :price, :city, {images: []})
end
<div class="property-image">
<h2>Cargar Imagenes</h2>
<div class="field">
<%= f.file_field :images, multiple: true %>
</div>
</div>
有线索吗??
在您的迁移过程中,您尝试创建一个 "images" 列并将 json 作为数据类型。
正如您的 schema.rb 告诉您的那样,您使用的数据库管理器无法做到这一点。
我假设您使用的是 Sqlite3,因为它是 rails 上的默认 dbm。 事实上,Sqlite 无法处理 json 数据类型
一个好的解决方案是将您的数据库从 Sqlite 迁移到 PostgreSql,因为它支持 json 并且它是 heroku 上的默认数据库管理器。
如果您在本地计算机上,要从 Sqlite3 迁移到 PG:
1 - 在本地机器上安装 Postgresql。根据您的 OS,您可以在这里找到您需要的内容:https://www.postgresql.org/download/
2- 更改您的 gem 文件 :
删除:
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
添加:
# Use postgres as the database for Active Record
gem 'pg'
3 - 运行 在你的命令行上
$ bundle install
它将在您的 Rails 应用程序上安装 postgresql gem
4 - 更新您的 database.yml 文件以使其适用于您的新数据库
从
更改您以前的文件# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
对此:
#
# gem install pg
#
# Ensure the PostgreSql gem is defined in your Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
password: the_password_you_set_when_installing_postgresql_on_your_machine
username: postgres
host: localhost
development:
<<: *default
database: db/development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test
5 - 现在您只需从命令行进行迁移: 首先重置数据库
$ rake db:setup
然后运行迁移
$ rake db:migrate
6 - 最后重启你的服务器
$ rails server