gpt4 book ai didi

ruby-on-rails - 载波,保存记录?

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:41 26 4
gpt4 key购买 nike

您好,我正在关注 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'

编辑 1

您好,感谢您的帮助!

在这个 Controller 中我已经有了一个创建方法我正在使用它让管理员发布某种文章......我想添加多张图片

这是我的方法:

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'

再次感谢:)

编辑2

我的模式:

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

结束

另外,因为我正在使用管理,所以我必须在 View 中创建一个特殊字段 views/fields/carrierwave_fields/_form.html.slim

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

这是我的 Controller 进度

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

编辑 3

我注意到在我的服务器中有一个 Unpermitted Parameter: images 但如果您查看上面的 Controller ,图像是允许的...我允许一组图像,它似乎不喜欢它......我该如何设置?

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)

编辑 4

按照建议,我在 controller_progresses.rb 创建方法中添加了 @progress = Progress.new(progress_params)

并且还将私有(private)方法更改为:

  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

编辑5

我相信我发现了错误的来源...我正在使用 administrate gem所以我有一个包含 admin/progresses_controller.rb 的文件夹在那里我必须覆盖 create 方法因为在 controllers/progresses_controller.rbcreate 方法只保存一个对象...(我相信……)

所以我必须完成这个:

  module Admin
class ProgressesController < Admin::ApplicationController

def create
end

end
end

编辑几乎完成!

所以我在Adminstrate的Module admin中添加了这个

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方法 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

在我看来... 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"

最佳答案

在用户模型上,你在一个字段上安装了一个载波 uploader ......假设是“头像”。因此,在每个用户实例上,您都有一个“头像”字段。

现在,当您创建一个 User 实例时 User.new(params[:user]) 如果在 params[:user] 中您有一个条目 params[:user][:avatar ] = "...",carrierwave 将保存您的图像,将生成 url 等。[所有这些都基于您在 carrierwave uploader 中开发的内容]

附言该代码片段可以在 UsersController 的创建方法中使用:)

关于ruby-on-rails - 载波,保存记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com