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 method
because in the controllers/progresses_controller.rb the create method
only 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"