Rails unpermitted parameters files when uploading using carrierwave -
i trying upload using carrierwave facing error ..when select file , click on upload still says it can't blank , on console 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 |file| #@file_extension=file.content_type.split('/')[1] doc = upload.new(tageline: file ) #save method save content in database doc.save! end end end
[uploads/new.html.erb]
<%= form_for @upload,html: { multipart: true } |f| %> <% if @upload.errors.any? %> <div id="errors"> <h2><%= pluralize(@upload.errors.count, "error") %> prevented post saving:</h2> <ul> <% @upload.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= f.label :title %><br> <%= f.text_field :title %><br> <br> <%= f.label :description %><br> <%= f.text_field :description %><br> <br> <%= f.label :files %><br> <%= f.file_field :files%><br> <br> <%= f.submit %> <% end %>
[upload_migration]
class createuploads < activerecord::migration def change create_table :uploads |t| t.string :title, null: false t.string :description, null: false t.string :tageline, null: false t.timestamps end end end
[console]
started post "/uploads" 127.0.0.1 @ 2016-02-08 14:18:54 +0530 processing uploadscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"1yars4bpwg6w5rakns/bocf24tezalqwuxeehjz04ny=", "upload"=>{"title"=>"mjkhhjkkj", "description"=>"jhhjnh", "files"=>#<actiondispatch::http::uploadedfile:0x000000053ec6c8 @tempfile=#<tempfile:/tmp/rackmultipart20160208-10624-40whhz>, @original_filename="design patterns in ruby (2007).pdf", @content_type="application/pdf", @headers="content-disposition: form-data; name=\"upload[files]\"; filename=\"design patterns in ruby (2007).pdf\"\r\ncontent-type: application/pdf\r\n">}, "commit"=>"create upload"} user load (0.3ms) select `users`.* `users` `users`.`id` = 4 order `users`.`id` asc limit 1 unpermitted parameters: files (0.1ms) begin (0.1ms) rollback rendered uploads/new.html.erb within layouts/application (2.7ms) completed 200 ok in 86ms (views: 82.3ms | activerecord: 0.5ms)
[tageline_uploader]
# encoding: utf-8 class tagelineuploader < carrierwave::uploader::base # include rmagick or minimagick support: # include carrierwave::rmagick # include carrierwave::minimagick # choose kind of storage use uploader: storage :file # storage :fog # override directory uploaded files stored. # sensible default uploaders meant mounted: def store_dir "uploadss/post/#{model.id}" end # provide default url default if there hasn't been file uploaded: # def default_url # # rails 3.1+ asset pipeline compatibility: # # actioncontroller::base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # process files uploaded: # process :scale => [200, 300] # # def scale(width, height) # # # end # create different versions of uploaded files: # version :thumb # process :resize_to_fit => [50, 50] # end # add white list of extensions allowed uploaded. # images might use this: # def extension_white_list # %w(jpg jpeg gif png) # end # override filename of uploaded files: # avoid using model.id or version_name here, see uploader/store.rb details. # def filename # "something.jpg" if original_filename # end end
you should add parameter :tageline
params_abc
method.
def params_abc params.require(:upload).permit(:tageline, :title, :description) end
also need add :tageline
upload field because don't have :upload
field in database.
mount_uploader :tageline, uploaduploader
no need have upload
method in model upload
, create new upload
object per each file, remove it.
also should remove line controller:
upload.upload(params[:upload][:files])
and change
<%= f.label :tageline %><br> <%= f.file_field :tageline %><br>
in view
Comments
Post a Comment