您好我正在关注carrierwave documentation,但我该将这部分放在哪里?
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'
您好,感谢您的帮助!
在这个控制器中我已经有了一个create方法 我正在使用它为管理员发布某种文章... 我想添加多个图像
这是我的方法:
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
端
如何添加第一个代码段?
它应该看起来更像是这样:
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'
再次感谢:)
我的架构:
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
端
另外因为我使用的是administrate,我必须在视图中创建一个特殊字段views/fields/carrierwave_fields/_form.html.slim
= f.label :images
= f.file_field :images, multiple: true
这是我的控制器进展
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
我在服务器中注意到Unpermitted Parameter: images
,但是如果您在控制器上方查看图像是允许的......
我允许一系列图像,似乎它不喜欢它......我怎么设置它?
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)
根据建议,我在 controller_progresses.rb 创建方法中添加了@progress = Progress.new(progress_params)
并且还将私有方法更改为:
def progress_params
params.require(:progress).permit(:title, :date, :content, :images)
end
另外,我发现 sqlite3 无法支持数组,因此我将数据库更改为PosgreSQL 现在我的架构很好...... 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
我相信我发现了我的错误来自......
我正在使用administrate gem所以我有一个 admin / progresses_controller.rb 的文件夹
在那里我必须覆盖create method
,因为在 controllers / progress_controller.rb 中create method
只保存一个对象......(我相信......)
所以我必须完成这个:
module Admin
class ProgressesController < Admin::ApplicationController
def create
end
end
end
所以我把它添加到了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
我留下了create Method
progress_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
在我的观点中...... show.html.slim 我不知道为什么,但我可以显示任何图片......
.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
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"
答案 0 :(得分:0)
在用户模型上,您在一个字段上安装了一个载波上传器......让我们说“头像”。因此,在每个用户实例上,您都有一个“头像”字段。
现在,当您创建用户实例User.new(params[:user])
时,如果在params [:user]中您有一个条目params[:user][:avatar] = "..."
,则carrierwave将保存您的图像,将生成网址等[所有基于什么你在你的载波上传中加入了
P.S。这段代码可以在createController上的UsersController中使用:)
答案 1 :(得分: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
# 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