0

In my project i pass javascript array to rails controller using ajax. It's work fine. That javascript array contains upload image names. In my postgresql database i have array column name images. In rails controller i try like this

@usr_vendor_web_slide.images = params[:A]
@usr_vendor_web_slide.save

params are pass to controller and it's alredy work.

*******["sm12.png", "sm13.png", "sm11.png"]******

but in database images name array not pass to the database. In terminal

["images", #<struct 
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array::Data encoder=#
<PG::TextEncoder::Array:0x5
7c6b10 "string[]"  elements_type=nil needs quotation>, values=[]>]]

appreciate your ideas and helps

3
  • how did you create array type column? did you make it by migration or by serialize it in model? Commented Jan 11, 2018 at 6:03
  • @Gabbar by migration Commented Jan 11, 2018 at 6:05
  • you should not do this like that, reason being via migration array type column not works in mysql db. i m writing the solution below. Commented Jan 11, 2018 at 6:08

1 Answer 1

1

1- write a migration to add column images of text type: -

add_column :your_table_name, :images, :text

2- in model serialize this column as array type

Class YourModel < ActiveRecord::Base
  serialize :images, Array
end

3- Now if you will check at rails colsole

YourModel.new

=> #<YourModel:0x00000005bdac68
 id: nil,
  ......
 images: [],
 ....>

4- now you can save data into this array type column:-

   @usr_vendor_web_slide.images.push(params[:A])
   @usr_vendor_web_slide.save

Note: array type column using migration may not work with some database, but by serialize will work perfectly.

Sign up to request clarification or add additional context in comments.

4 Comments

it work fine. but after solved it i have another problem. If you know anything about this column edit function. help me to settle this. Again thanks in advance
@ThilankaUmayangaJayarathna what you mean for edit function? you want to replace the images names of existing image? am i right?
yes. you're right. if you have any link or something to refer for edit function @Gabbar
@ThilankaUmayangaJayarathna hmm i believe it would be just array delete existing content and add push new image string in edit function and its nothing but simple array function, and you can do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.