Question

I am trying to iterate through a nested hash to access two separate fields. This is what I am trying to do:

  1. Check to see if :is_cover_photo is true
  2. if :is_cover_photo is true then I want to call the update method on @point_of_interest.

I keep getting an error thrown and cannot figure out how to accomplish this task.

This is error I am getting: "no implicit conversion of Symbol into Integer"

This is in my update method of point_of_interests_controller:

  def update
    @point_of_interest = PointOfInterest.find(params[:id])

    if @point_of_interest.update(params[:point_of_interest].permit(:title, :caption, :website, :summary, :latitude, :longitude, :cover_photo, albums_attributes: [:id, :cover_photo, :title, :is_cover_photo]))
      params[:point_of_interest][:albums_attributes].each do |key, value|
        value.each do |k, v|
          if k[:is_cover_photo] && k[:cover_photo].blank?
            @point_of_interest.update(:cover_photo => k[:cover_photo])
          end   
        end  
      end
      redirect_to @point_of_interest
    else
      render 'edit'
    end
    end

Here are the parameters being passed:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"XGvgfxFHZKpiE8eGqc+55qOlSSk9LYFGdywWyABezXo=",
 "point_of_interest"=>{"title"=>"WolfieOne",
 "latitude"=>"33.577836",
 "longitude"=>"-85.074692",
 "caption"=>"CaptionOne",
 "website"=>"google.com",
 "summary"=>"This is a test summary.",
 "albums_attributes"=>{"0"=>{"title"=>"Album Uno",
 "cover_photo"=>#<ActionDispatch::Http::UploadedFile:0x000000113ae4c0 @tempfile=#<Tempfile:C:/Users/Samuel/AppData/Local/Temp/RackMultipart20140220-7188-4ptfo1>,
 @original_filename="Lighthouse.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"point_of_interest[albums_attributes][0][cover_photo]\"; filename=\"Lighthouse.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 "is_cover_photo"=>"1",
 "id"=>"23"},
 "1"=>{"title"=>"Album 2",
 "is_cover_photo"=>"0",
 "id"=>"24"},
 "2"=>{"title"=>"The Best Album Alive",
 "is_cover_photo"=>"0",
 "id"=>"30"}}},
 "commit"=>"Submit Wolfie",
 "id"=>"15"}

Any help on this would be greatly appreciated.

Update

This resolved the issue for me:Thanks to Coenwulf

params[:point_of_interest][:albums_attributes].each do |key, value|
  if value[:is_cover_photo] && value[:cover_photo].present?
    @point_of_interest.update(:cover_photo => value[:cover_photo])
  end
end
Was it helpful?

Solution

Try replacing the contents of the first if block with:

params[:point_of_interest][:albums_attributes].each do |key, value|
  if value[:is_cover_photo] && value[:cover_photo].present?
    @point_of_interest.update(:cover_photo => value[:cover_photo])
  end
end
redirect_to @point_of_interest

EDIT: Edited to use present? instead of blank? based on comments from OP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top