0

Im stuck in a very simple problem. I am receiving an array of parameters in my view and want to iterate over the values.

This is the print of my params:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"dfOQVuXlQriII3akiGCSuMIf4i2B8c1/OX02nd6Dhy0ZKzHkhiXxlcXKCJAMFHw0vhtNKKVYuLHFo22LGsy6UQ==", "album"=>{"name"=>"asdasd", "photos_media"=>["[{\"id\":\"a245b724845f447eb63dfbaa3fba173669b55fcdf7fb55fb634707ff0c1c\",\"filename\":\"BWT_eUmU.jfif\",\"content_type\":\"image/jpeg\",\"size\":56060},{\"id\":\"1bfb4188a126079f5069c5204f8df1c7169d0464f488385ef1f8d081fcda\",\"filename\":\"drafts.png\",\"content_type\":\"image/png\",\"size\":6029}]"]}, "commit"=>"Save Album"}

My album_params are like this:

  def album_params
    params.require(:album).permit(:name,  photos_media: [])
  end

And my form:

<%= form_for @album do |f| %>
  <div class="col-9">
    <div class="form-group row" >
      <div class="col-6">
        <label for="">Name:</label>
        <%= f.text_field :name %>
      </div>
    </div>
  </div>
  <div class="col-9">
    <div class="form-group row" >
      <div>
        <div class="progress" id='progress-bar' style='display:none;'>
          <div class="progress-bar progress-bar-striped active" role="progressbar" style="width: 0%"><h6>Loading...</h6>
            <span class="sr-only" id='progress-bar-text'></span>
          </div>
        </div>  
      </div>
      <div class="col-6">
        <label for="">Add or drag photos here:</label>
        <%= f.attachment_field :photos_media, multiple: true, direct: true, presigned: true %>
      </div>    
    </div>
  </div>

How can I iterate over photos_media? If I do a print on it like this:

 logger.debug("******SIZE*** #{album_params[:photos_media].size} ")

It says the size is 1. Like a big string.

What im doing wrong?

2
  • 1
    Look at your params, it is really a string "photos_media"=>["here goes long string with an array inside"]. Add code of the form, from which you get the params Commented Dec 11, 2018 at 17:30
  • Im editing and adding the view Commented Dec 11, 2018 at 17:32

1 Answer 1

1

As Vailisa says, photo_media contains an Array with one element that is a string:

"photos_media"=>["[{\"id\":\"a245b724845f447eb63dfbaa3fba173669b55fcdf7fb55fb634707ff0c1c\",\"filename\":\"BWT_eUmU.jfif\",\"content_type\":\"image/jpeg\",\"size\":56060},{\"id\":\"1bfb4188a126079f5069c5204f8df1c7169d0464f488385ef1f8d081fcda\",\"filename\":\"drafts.png\",\"content_type\":\"image/png\",\"size\":6029}]"]

Specifically, that string is a JSON string.

To parse that on the backend, you can try:

@photo_media_ary = JSON.parse(album_params[:album][:photos_media][0])
Sign up to request clarification or add additional context in comments.

Comments

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.