0

I have a Image model which belongs to Gallery. I have an additional/seperate form in my gallery edit page to upload images and associate them with the gallery. I am using Fog with Carrierwave to upload to Rackspace files.

The error I get is forbidden attributes, but it doesn't tell me which attribute/param is causing the problem. How can i further debug this??

Model

class Image < ActiveRecord::Base
  belongs_to :gallery

  mount_uploader :file, ImageUploader

  def to_jq_upload
    {
      "name" => read_attribute(:file),
      "size" => file.size,
      "url" => file.url,
      "thumb_url" => file.thumb.url,
      "delete_url" => image_path(:id => id),
      "delete_type" => "DELETE" 
    }
  end
end

Controller

class ImagesController < ApplicationController

  def create

    p_attr = params[:image]
    p_attr[:gallery_id] = params[:image][:gallery_id]
    p_attr[:file] = params[:image][:file].first if params[:image][:file].class == Array

    @image = Image.new(p_attr)
    if @image.save
      respond_to do |format|
        format.js
        format.html {  
          render :json => [@image.to_jq_upload].to_json, 
          :content_type => 'text/html',
          :layout => false
        }
        format.json {  
          render :json => { :files => [@image.to_jq_upload] }     
        }
      end
    else 
      render :json => [{:error => "custom_failure"}], :status => 304
    end

  end


  private

    def image_params
      params.require(:image).permit(:gallery_id, file:[])
    end

end

Form

<%= form_for(@gallery) do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :desc %><br>
    <%= f.text_field :desc %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 <%= form_for Image.new, :html => { :multipart => true, :id => "fileupload" } do |f| %>
            <%= f.file_field :file, :id => "add_images_field", :multiple => true %>
            <%= f.hidden_field :gallery_id, :value => @gallery.id %>
 <% end %>

Error

Started POST "/images" for 127.0.0.1 at 2014-10-02 21:25:50 -0400
Processing by ImagesController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RUCmH41pGzkzWurqqeQHJgsn3ejtrTbscdiGey9036E=", "image"=>{"gallery_id"=>"1", "file"=>[#<ActionDispatch::Http::UploadedFile:0x007ffbd6e2dbd0 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-83940-w9i67p>, @original_filename="10648423_1503708756533719_4977582933706996777_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10648423_1503708756533719_4977582933706996777_o.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
P_ATTR = {"gallery_id"=>"1", "file"=>#<ActionDispatch::Http::UploadedFile:0x007ffbd6e2dbd0 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-83940-w9i67p>, @original_filename="10648423_1503708756533719_4977582933706996777_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10648423_1503708756533719_4977582933706996777_o.jpg\"\r\nContent-Type: image/jpeg\r\n">}
Completed 500 Internal Server Error in 1ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  app/controllers/images_controller.rb:24:in `create'
Joel Grannas
  • 2,016
  • 2
  • 24
  • 46

1 Answers1

0

So... the solution is as follows... But i still don't understand how the same code works in my other app, using the same rails version and I didnt have to use this permit! line.

p_attr.permit!

class ImagesController < ApplicationController
  def create

    p_attr = params[:image]
    p_attr[:gallery_id] = params[:image][:gallery_id]
    p_attr[:file] = params[:image][:file].first if params[:image][:file].class == Array

    p_attr.permit!
    @image = Image.new(p_attr)
    if @image.save
      respond_to do |format|
        format.js
        format.html {  
          render :json => [@image.to_jq_upload].to_json, 
          :content_type => 'text/html',
          :layout => false
        }
        format.json {  
          render :json => { :files => [@image.to_jq_upload] }     
        }
      end
    else 
      render :json => [{:error => "custom_failure"}], :status => 304
    end

  end
Joel Grannas
  • 2,016
  • 2
  • 24
  • 46