aniket February 2016

Rails unpermitted parameters files when uploading using carrierwave

I am trying to upload using carrierwave but I am facing an error ..when i select the file and click on upload still it says it can't be blank and on console it says unpermitted parameters files .
[upload_controller.rb]

class UploadsController < ApplicationController
  before_action :authenticate_user! 

  def index
    @uploads=Upload.all
  end

  def new
    @upload=Upload.new
  end


  def create

    @upload=Upload.new(params_abc)

     if  @upload.save
        Upload.upload(params[:upload][:files])  
        redirect_to  @upload
     else
       render 'new'
     end

  end

private

  def params_abc
        params.require(:upload).permit(:title,:description)
  end

end


[upload.rb]

class Upload < ActiveRecord::Base

    validates :description,  presence: true
    validates :title,  presence: true
    validates :tageline, presence: true

    mount_uploader :upload, UploadUploader 

    def self.upload(files)  
        files.each do |file|    
        #@file_extension=file.content_type.split('/')[1]            

            doc = Upload.new(tageline: file )
            #save is a method which will save the content in the database 
            doc.save!
        end
    end

end


[uploads/new.html.erb]

<%= form_for @upload,html: { multipart: true } do |f| %>
    <% if @upload.errors.any? %>
        <div id="errors">
            <h2><%= pluralize(@upload.errors.count, "error") %> prevented this post from saving:</h2>
            <ul>
                <% @upload.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
    <br>

    <%=         

Answers


Andrey Konoplenko February 2016

You should add parameter :tageline to your params_abc method.

def params_abc
  params.require(:upload).permit(:tageline, :title, :description)
end

Also you need to add :tageline as upload field because you don't have :upload field in your database.

mount_uploader :tageline, UploadUploader

No need to have upload method in your model Upload and create new Upload object per each file, just remove it.

Also you should remove this line from controller:

Upload.upload(params[:upload][:files])

And change

<%= f.label :tageline %><br>
<%= f.file_field :tageline %><br>

in your view


Ilya February 2016

You must permit files param:

def params_abc
  params.require(:upload).permit(:title,:description, :files)
end

More about permitted params here

Post Status

Asked in February 2016
Viewed 3,576 times
Voted 7
Answered 2 times

Search




Leave an answer