我正在尝试使用carrierwave上传多张图片,当我尝试保存时会收到此错误
can't cast Array to
告诉我错误在控制器中
if @property.save
我不知道它是否相关,但是当我添加此迁移时
rails g migration add_images_to_properties images:json
架构为属性表
提出了此问题# Could not dump table "properties" because of following NoMethodError
其他一切都井井有条......
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>
任何线索??
答案 0 :(得分:1)
在您的迁移中,您尝试创建了一个&#34;图像&#34;以json作为数据类型的列。
当您的schema.rb告诉您时,您使用的数据库管理器无法执行此操作。
我假设您正在使用Sqlite3,因为它是rails上的默认dbm。 实际上,Sqlite无法处理json数据类型
一个好的解决方案是将数据库从Sqlite迁移到PostgreSql,因为它支持json,因为它是heroku上的默认数据库管理器。
如果你在本地机器上,要从Sqlite3迁移到PG:
1 - 在本地计算机上安装Postgresql。根据您的操作系统,您可以在此处找到所需内容:https://www.postgresql.org/download/
2-更改 gemfile :
删除:
# 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 - 现在您只需从命令行进行迁移: 首先重置db
$ rake db:setup
然后运行迁移
$ rake db:migrate
6 - 最终重启服务器
$ rails server