2016-06-28 11 views
0

我想使用carrierwave上傳多張圖片,並在嘗試保存我得到這個錯誤無法施展陣列到 - 多張影像carrierwave

can't cast Array to 

告訴我的錯誤是在控制器

if @property.save 

我不知道,如果它的相關的,但是當我加入這個遷移

rails g migration add_images_to_properties images:json 

架構目前ED這個問題的屬性表

# Could not dump table "properties" because of following NoMethodError 

未定義的方法`[]」爲零: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> 

任何線索??

+0

你能:1.添加你使用的是確切的代碼?這段代碼遍佈各處(例如''property property''實際上是'def property_params',或者它在其他地方浮動?)。 2.準確地寫下你在做什麼導致錯誤和完整的錯誤。 – Julie

回答

1

在您的遷移中,您嘗試使用json作爲數據類型創建「圖像」列。

由於您的schema.rb證明了您正在使用的數據庫管理器未能如願。

我假設你使用Sqlite3,因爲它是rails上的默認dbm。 事實上,Sqlite無法處理json數據類型

一個好的解決方案是將您的數據庫從Sqlite遷移到PostgreSql,因爲它支持json,因爲它是heroku上的默認數據庫管理器。

如果你的本地計算機上,以從遷移到SQLITE3 PG:

1 - 你的本地計算機上安裝Postgersql。根據您的操作系統,你可以找到你所需要的是什麼: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應用程序

4安裝PostgreSQL的寶石 - 更新數據庫。陽明海運文件,以使其與新的DB

變化工作從

# 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 
相關問題