Ruby on rails Carrierwave,保存记录?
你好,我在跟踪,但我必须把这部分放在哪里Ruby on rails Carrierwave,保存记录?,ruby-on-rails,ruby,carrierwave,Ruby On Rails,Ruby,Carrierwave,你好,我在跟踪,但我必须把这部分放在哪里 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 你好,谢谢你的帮助 在这个控制器中,我已经有了一个create方法 我使用它的管理员发布一些文章
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
你好,谢谢你的帮助
在这个控制器中,我已经有了一个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'
再次感谢:)
编辑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
结束
另外,因为我使用的是administrate,所以我必须在viewsviews/fields/carrierwave\u fields/\u 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
编辑4
根据建议,我在我的controller\u progress.rb创建方法中添加了@progress=progress.new(progress\u params)
并将私有方法更改为:
def progress_params
params.require(:progress).permit(:title, :date, :content, :images)
end
另外,我发现sqlite3无法支持数组,所以我将数据库更改为PosgreSQL
现在我的模式很好。。。。
编辑5
我相信我找到了我的错误来源。。。
我正在使用,因此我有一个带有admin/progress\u controller.rb的文件夹
在那里,我必须覆盖创建方法
,因为在控制器/progress\u controller.rb中创建方法
只保存一个对象…(我相信…)
因此,我必须完成以下工作:
module Admin
class ProgressesController < Admin::ApplicationController
def create
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"
在用户模型上,您在字段上安装了carrierwave上载程序。。。比如说“阿凡达”。因此,在每个用户实例上都有一个“化身”字段 现在,当您创建一个用户实例时,
User.new(params[:User])
如果在params[:User]中有一个条目,params[:User][:avatar]=“…”
,carrierwave将保存您的图像,生成url等[所有这些都基于您在carrierwave上载程序中的设置]
另外,这段代码可以在创建方法的UsersController中使用:)hi,我不必创建模型image.rb?我错过什么了吗?我添加了一个迁移到进度表以添加图像您可以在类progress中使用mount_uploader,但它对于一个对象进度只有一个图像。无法保存图像[0]、图像[1]、图像[2]……mount_uploaders:images和我添加的multiple:true(允许多个图像)的形式如何?请检查此视频以检查您在本主题中的实现。这正是我所做的。。。见马蒂亚斯的答案。。。不幸的是,我总是有同样的问题,加上我注意到了不允许的参数图像,请参见我的编辑
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
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
.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
.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"
# 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