Search code examples
ruby-on-rails-4parametersparameter-passingnested-attributes

Unpermitted parameter for photos


I'm on rails 4 and using carrierwave, and I'm trying let users upload photos that belong to specific objects. The parent object is Property, and the child object is Photo. I did this with an eye towards allowing a Property to have multiple photos.

I would like to be able to upload a photo while on the Property edit page. The uploaded photo will have a foreign key that relates to the property_id so I can identify which property it belongs to (hence the hidden field).

My problem is that I keep getting an "Unpermitted parameter: photos" message in my logs that's keeping me from creating the Photo object.

My form looks like this

<%= form_for @property, html: { multipart: true } do |f| %>
  <%= render "property_form_fields", f: f %>
      <%= f.fields_for :photos do |photo| %>
        <p>Add photos:</p>
        <%= photo.file_field :photo_file_name%>
        <%= photo.hidden_field :property_id, :value => @property.id%>  
      <% end %>            
    <%= f.submit "Update property", class: 'btn btn-default' %>
<% end %>

Photo model:

class Photo < ActiveRecord::Base
  belongs_to :property

  mount_uploader :photo_file_name, PhotoUploader

  validate  :picture_size

  private

    # Validates the size of an uploaded picture.
    def picture_size
      if picture.size > 5.megabytes
        errors.add(:picture, "should be less than 5MB")
      end
    end
end

Property model

class Property < ActiveRecord::Base
  has_many :photos
end

Here is my Property Controller

  def edit
    @property = Property.find(params[:id])
    @photos = Photo.where(:property_id => @property.id)

  end

  def update
    @property = Property.find(params[:id])
    if @property.update_attributes(property_params)
      flash[:success] = "Property was successfully updated."
      redirect_to properties_path
    else
      flash[:error] = "Property was not created"
      render edit_property_path
    end
 private
    def property_params
      params.require(:property).permit(:property_name, photos_attributes: [:id, :photo_file_name,:property_id])
    end

end

Here are my logs when I try to add a photo

Started PATCH "/properties/19" for 108.41.84.27 at 2016-07-08 19:59:24 +0000
Processing by PropertiesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ucP...", "property"=>{"property_name"=>"abc", "photos"=>{"photo_file_name"=>#<ActionDispatch::Http::UploadedFile:0x007fe11f0a5598 @tempfile=#<Tempfile:/home/ubuntu/workspace/RackMultipart20160708-6915-1havz2i.jpg>, @original_filename="1554439_686460264746128_2054601640279902551_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"property[photos][photo_file_name]\"; filename=\"1554439_686460264746128_2054601640279902551_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, "property_id"=>"19"}}, "commit"=>"Update property", "id"=>"19"}
  Manager Load (0.3ms)  SELECT  "managers".* FROM "managers" WHERE "managers"."id" = ?  ORDER BY "managers"."id" ASC LIMIT 1  [["id", 1]]
  Property Load (0.2ms)  SELECT  "properties".* FROM "properties" WHERE "properties"."id" = ? LIMIT 1  [["id", 19]]
Unpermitted parameter: photos
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to https://project-apple-mcl282.c9users.io/properties
Completed 302 Found in 12ms (ActiveRecord: 0.7ms)

Solution

  • your Property model needs to accept nested attributes for photos:

    class Property < ActiveRecord::Base
      has_many :photos
      accepts_nested_attributes_for :photos
    end