ruby-on-railsrubypostmanruby-on-rails-6

Rails API - Getting Unpermitted params uploading files


I'm trying to make a POST request to create a restaurant profile uploading two images in each of two attributes(pictures and menus).

I'm using Postman(form-data) to test it and I'm geting Unpermitted parameters: :pictures, :menus, :format.

I'll show the code bellow but I want to say that when I use the Strong Parameters like this params.permit(:name, ..., :pictures, :menus) , it works but only get one image to each attribute.

Model(restaurant.rb)

class Restaurant < ApplicationRecord
  include Rails.application.routes.url_helpers

  has_many_attached :pictures
  has_many_attached :menus

end

Controller(restaurant_controller.rb)

module Api
  module V1
    class RestaurantsController < Api::V1::ApiController

      def create
        @restaurant = Restaurant.new(restaurant_params)
        render json: @restaurant, status: :created if @restaurant.save!
      end

      private

      def restaurant_params
        params.permit(
          :name, :status, :address,
          :neighborhood, :city, :phone,
          :other_phone, :cost, :timmings,
          :happy_hour, :coffee, :delivery,
          :ac, :wifi, :cards, :tickets,
          :outside_place, :club, :vegetarian,
          :map, :cuisine_id, :latitude,
          :longitude, pictures: [], menus: []
        )
      end

    end
  end
end

Serializer(restaurant_serializer.rb)

class RestaurantSerializer < ActiveModel::Serializer
  include Rails.application.routes.url_helpers
  attributes :id, :name, :pictures, :menus

  def pictures
    arr = []
    if object.pictures.attached?
      object.pictures.each do |picture|
        arr << {
          url: rails_blob_url(picture)
        }
      end
    end
    arr
  end

  def menus
    arr = []
    if object.menus.attached?
      object.menus.each do |menu|
        arr << {
          url: rails_blob_url(menu)
        }
      end
    end
    arr
  end
end

Console(rails server + binding.pry)

    19: def create
 => 20:   binding.pry
    21:   @restaurant = Restaurant.new(restaurant_params)
    22:   render json: @restaurant, status: :created if @restaurant.save!
    23: end

[1] pry(#<Api::V1::RestaurantsController>)> restaurant_params
Unpermitted parameters: :pictures, :menus, :format
=> <ActionController::Parameters {"name"=>"Italo's Palace", "status"=>"available", "address"=>"Av. Dr. Arhur, 23", "neighborhood"=>"Copacabana", "city"=>"Santos", "phone"=>"13 3214-1221", "other_phone"=>"13 3232-1290", "cost"=>"59.99", "timmings"=>"Seg-Qui 12h às 22h, Sex-Sáb 11h 0h & Dom 11h às 16h", "happy_hour"=>"Sex 18h às 21h", "coffee"=>"1", "delivery"=>"1", "ac"=>"1", "wifi"=>"1", "cards"=>"1", "tickets"=>"1", "outside_place"=>"1", "club"=>"1", "vegetarian"=>"1", "cuisine_id"=>"1", "latitude"=>"-23.989099", "longitude"=>"-46.299527"} permitted: true>

UPDATE:

Postman request header enter image description here

Postman request body with response(image's urls should be included in pictures and menus arrays):

enter image description here

Console: enter image description here


Solution

  • The problem was on Postman, it was sending only one file. I fixed adding [] after pictures and menus params. As shown in the picture: enter image description here