2

Hello I am following carrierwave documentation but where do I have to put this part ?

u = User.new(params[:user])
u.save!
u.avatars[0].url # => '/url/to/file.png'
u.avatars[0].current_path # => 'path/to/file.png'
u.avatars[0].identifier # => 'file.png'

EDIT 1

Hello thanks for your help !

In this controller I already have a create method I am using it for the admin to post some kind of articles... I wanna add the multiple images

here is my method:

def create
respond_to do |format|
  if  @progress.save!
      format.html { redirect_to @progress, notice: 'progress was successfully created.' }
      format.json { render :show, status: :created, location: @progress }
  else
    format.html { render :new }
    format.json { render json: @progress.errors, status: :unprocessable_entity }
  end
end

end

How do I add the first snippet ?

it should looks more like this then:

  pr = Progress.new(params[:progress])
  pr.save!
  pr.images[0].url # => '/url/to/file.png'
  pr.images[0].current_path # => 'path/to/file.png'
  pr.images[0].identifier # => 'file.png'

thanks again :)

EDIT 2

my schema:

ActiveRecord::Schema.define(version: 20160813155729) do

  create_table "progresses", force: :cascade do |t|
   t.string   "title"
   t.text     "content"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.string   "date"
   t.string   "images"
  end

end

Also because I am using administrate, I had to create a special field in views views/fields/carrierwave_fields/_form.html.slim

= f.label :images
= f.file_field :images, multiple: true

And here is my controller progresses

class ProgressesController < ApplicationController

  def index
   @progresses = Progress.all
  end

 def show
  @progress = Progress.find(params[:id])
 end

 def new
  @progress = Progress.new
 end

def edit
  @progress = Progress.find(params[:id])
end

def create
  @progress = Progress.new(progress_params)
  respond_to do |format|

   if  @progress.save!
      format.html { redirect_to @progress, notice: 'progress was       successfully created.' }
      format.json { render :show, status: :created, location: @progress }
  else
    format.html { render :new }
    format.json { render json: @progress.errors, status: :unprocessable_entity }
   end
 end
end

def update
  @progress = Progress.find(params[:id])
  if @progress.update(progress_params)
    redirect_to progress_path(@progress.id)
  else
   render :progress
  end
end

private
 def progress_params
  params.require(:progress).permit(:title, :date, :content, :images})
 end
end

EDIT 3

I've noticed in my server an Unpermitted Parameter: images but if you look in the controller above images are permitted... I permit an array of images, and it seems that it doesn't like it... how can I set this?

Started POST "/admin/progresses" for ::1 at 2016-08-14 23:11:14 +0200
Processing by Admin::ProgressesController#create as HTML
Parameters: {"utf8"=>"✓",        "authenticity_token"=>"BU7+nt4t86UyuHLI9ECY7NK8eYf2KE3hhv8M3oe23WlcS7faq70v  Xjpg4BRTzcMAyIDMD9D9idLeQ3tJbBEpRw==", "progress"=>{"title"=>"Test", "date"=>"14-08-2016", "content"=>"Donec venenatis vulputate lorem. Aenean   vulputate eleifend tellus.\r\n\r\nVestibulum volutpat pretium libero. In ac   felis quis tortor malesuada pretium.", "images"=>[#<ActionDispatch::Http::UploadedFile:0x007fd5da259060 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160814-46079-1wwzmsj.jpg>, @original_filename="12189611_10153366959293347_3053265627768711994_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[images][]\"; filename=\"12189611_10153366959293347_3053265627768711994_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fd5da259038 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160814-46079-t7l6bs.jpg>, @original_filename="12191872_10153366957043347_4735626737602785127_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[images][]\"; filename=\"12191872_10153366957043347_4735626737602785127_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fd5da258e30 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160814-46079-1erm3j5.jpg>, @original_filename="12915103_10208497250246588_1486835977_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[images][]\"; filename=\"12915103_10208497250246588_1486835977_o.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fd5da258d90 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160814-46079-6jbt5o.jpg>, @original_filename="13833402_10153984283243347_1465763991_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[images][]\"; filename=\"13833402_10153984283243347_1465763991_o.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Create Progress"}
Unpermitted parameter: images
(0.1ms)  begin transaction
SQL (0.4ms)  INSERT INTO "progresses" ("title", "date", "content",  "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["title", "Test"], ["date", "14-08-2016"], ["content", "Donec venenatis vulputate lorem.    Aenean vulputate eleifend tellus.\r\n\r\nVestibulum volutpat pretium libero. In ac felis quis tortor malesuada pretium."], ["created_at", "2016-08-14 21:11:14.199571"], ["updated_at", "2016-08-14 21:11:14.199571"]]
 (0.7ms)  commit transaction
 Redirected to http://localhost:3000/admin/progresses/1
 Completed 302 Found in 6ms (ActiveRecord: 1.1ms)

EDIT 4

As suggested I added the @progress = Progress.new(progress_params)in my controller_progresses.rb create method

And also changed the private method to:

  def progress_params
   params.require(:progress).permit(:title, :date, :content, :images)
 end

Also, I've found out that sqlite3 couldn't suppport array, so I have changed my database to PosgreSQL now my schema is fine.... My schema didn't display, so I asked here

ActiveRecord::Schema.define(version: 20160815134638) do

  # These are extensions that must be enabled in order to support this   database
  enable_extension "plpgsql"

 create_table "progresses", force: :cascade do |t|
    t.string   "title"
    t.string   "date"
    t.text     "content"
    t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.json     "images"
 end
end

Edit 5

I believe I found out where my error comes from... I am using administrate gem so I have a folder with admin/progresses_controller.rb in there I have to override the create methodbecause in the controllers/progresses_controller.rb the create methodonly save one object...( I belielve....)

SO I must complete this:

  module Admin
   class ProgressesController < Admin::ApplicationController

    def create
    end

   end
 end

EDIT ALMOST DONE !

So I added this to the Module admin of Administrate

module Admin
 class ProgressesController < Admin::ApplicationController
def create

   @progress = Progress.new(progress_params)
   @progress.save!
   @progress.images[0].url # => '/url/to/file.png'
   @progress.images[0].current_path # => 'path/to/file.png'
   @progress.images[0].identifier # => 'f

   if @progress.save!
     redirect_to admin_progresses_path, notice: "New article created!"
   end 
    # I have to imporove here ! but now images display
  end

  private
   def progress_params
    params.require(:progress).permit(:title, :date, :content, { images: [] } )
    end
   end
 end

I left a part of my create Method progresses_controller.rb

  def create
# @progress = Progress.new(progress_params)

respond_to do |format|
  if @progress.save
    format.html { redirect_to @progress, notice: 'progress was successfully created.' }
    format.json { render :show, status: :created, location: @progress }
  else
    format.html { render :new }
    format.json { render json: @progress.errors, status: :unprocessable_entity }
   end
  end
end

In my views... show.html.slim I don't know why but I can display any image...

.container
 .section
  .col-xs-12
   h2 = @progress.date
   h3 = @progress.title

   p  = simple_format(@progress.content)
   br
  .col-xs-12
    .carousel-inner
      .item.active
        =image_tag @progress.images

in index.html.slim

.container
 .section
  .col-xs-12
  h1 
   |Work In Progress  [
   = @progresses.count 
   | ]
 hr

 - @progresses.each do |progress|
   h3 = progress.title
   h3 = progress.date

   =image_tag @progress.images
   br

   p = simple_format(progress.content)
   = link_to "Open", progresses_path(@progress),
    class:"btn btn-primary btn-thumb"
Community
  • 1
  • 1

2 Answers2

0

On User model you mounted a carrierwave uploader on a field... let say "avatar". So, on every User instance you have an "avatar" field.

Now, when you create a User instance User.new(params[:user]) if in params[:user] you have an entry params[:user][:avatar] = "...", carrierwave will save your image, will produce url, etc. [all based on what you devinded in your carrierwave uploader]

P.S. That snippet of code could be used in UsersController on create method :)

kitz
  • 879
  • 2
  • 9
  • 24
0
# please show model images.rb I'll assuming that you have something like this

#images.rb
class Images < ActiveRecord::Base         
  mount_uploader :file, FileUploader

#progress.rb
class Progress < ActiveRecord::Base         
  has_many :images
end

# in your views/progress/_form.html.slim you have something like this

form_for @progress, method: :post do |f| 
  f.object.images.new if f.object.images.blank? 
  f.fields_for :images  do |image|
    image.file_field :file

# edit

# In your progress_params need to add the paramenter :images

private
    def progress_params
      params.require(:progress).permit(:title, :date, :content, :images) # not {images:[]}
    end

def create
#...
   @progress = Progress.new(progress_params)
#...
end
Breno Perucchi
  • 873
  • 7
  • 16
  • hi, I didn't have to create a model image.rb ? did I miss something? I added a migration to progress table in order to add images –  Aug 14 '16 at 15:37
  • You can use the mount_uploader in the class progress but it only have one image for one object progress. It will not possible to save images[0], images[1], images[2] .... – Breno Perucchi Aug 14 '16 at 15:43
  • what about the mount_uploaders :images and in the form I added the multiple: true ( to allow several images) –  Aug 14 '16 at 15:55
  • Please check this video to check your implementation [rails cast carrierwave](http://railscasts.com/episodes/253-carrierwave-file-uploads) – Breno Perucchi Aug 14 '16 at 16:01
  • In this topic there is exactly what I did... See Mathias answer... [link](http://stackoverflow.com/questions/21411988/rails-4-multiple-image-or-file-upload-using-carrierwave) Unfortunatly I always have the same problem plus I noticed the unpermitted parameter images, see my edit –  Aug 14 '16 at 21:55
  • I have added it to my create metohd and changed the private method progress_params as you suggested but still unpermitted parametters images –  Aug 15 '16 at 14:02
  • no... but I think I found where I was wrong, but I don't know how to set it :( I am using administrate gem, so I have an admin controller progresses where I have to override the methode create... And I don't know how to do this.... –  Aug 15 '16 at 18:33
  • I 've advanced a bit but I am stuck again, please can you help me? –  Aug 15 '16 at 22:03
  • Did you get a override Admin::ProgressController ? – Breno Perucchi Aug 16 '16 at 09:44
  • yes I did but now I can't display my images... should I open a new question it becomes messy here –  Aug 16 '16 at 10:31
  • I restarted from scratch using SSR method [link](http://stackoverflow.com/questions/21411988/rails-4-multiple-image-or-file-upload-using-carrierwave) –  Aug 16 '16 at 16:27