我创建了一个表单来创建文章,它包含标题,日期,内容,主图像和多重上传的特殊字段。图像随Carrierwave一起上传。
我有两个不同的模型和控制器可以使用此表单。 进度和 progress_attachments 。
由于我想附加多张图片以创建照片库,我创建了另一个控制器 progress_attachment 我跟着SSR explanations
当我提交表单时,我有这个错误
NoMethodError in ProgressesController#create
undefined method `[]' for nil:NilClass
即使该文章已发布......(我可以在rails控制台中找到它,如果我回到节目中)
我相信这个undefined method []
来自我在进度控制器中的创建方法,但我遗漏了一些东西,我不明白是什么......?
progresses_controller.rb
class ProgressesController < ApplicationController
def index
@progresses = Progress.all
end
def show
@progress = Progress.find(params[:id])
@progress_attachments = @progress.progress_attachments.all
end
def new
@progress = Progress.new
@progress_attachment = @progress.progress_attachments.build
end
def create
@progress = Progress.new(progress_params)
respond_to do |format|
if @progress.save
params[:progress_attachments]['image'].each do |a|
@progress_attachment = @progress.progress_attachments.create!(:image => a)
end
format.html { redirect_to progresses_path, notice: 'Progress was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
def update
respond_to do |format|
if @progress.update(article_params)
format.html { redirect_to @progress, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @progress }
else
format.html { render :edit }
format.json { render json: @progress.errors, status: :unprocessable_entity }
end
end
end
def destroy
@progress.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def progress_params
params.require(:progress).permit(:title, :content, :date, :main_image, progress_attachments_attributes: [:id, :progress_id, :image])
end
end
progress_attachments_controller.rb
class ProgressAttachmentsController < ApplicationController
before_action :set_progress_attachment, only: [:show, :edit, :update, :destroy]
def index
@progress_attachments = ProgressAttachment.all
end
def new
@progress_attachment = ProgressAttachment.new
end
def create
@progress_attachment = ProgressAttachment.new(progress_attachment_params)
@progress_attachment = @progress.progress_attachments.build
respond_to do |format|
if @progress_attachment.save
format.html { redirect_to @progress_attachment, notice: 'Progress attachment was successfully created.' }
format.json { render :show, status: :created, location: @progress_attachment }
else
format.html { render :new }
format.json { render json: @progress_attachment.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @progress_attachment.update(progress_attachment_params)
format.html { redirect_to @progress_attachment, notice: 'Progress attachment was successfully updated.' }
format.json { render :show, status: :ok, location: @progress_attachment }
else
format.html { render :edit }
format.json { render json: @progress_attachment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@progress_attachment.destroy
respond_to do |format|
format.html { redirect_to progress_attachments_url, notice: 'Progress attachment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_progress_attachment
@progress_attachment = ProgressAttachment.find(params[:id])
end
def progress_attachment_params
params.require(:progress_attachment).permit(:progress_id, :image)
end
end
views / progress / new.html.slim
中的表单 = simple_form_for(@progress, html: { multipart: true} ) do |f|
=f.input :title
=f.input :date
=f.input :content
=f.input :main_image
=f.simple_fields_for :progress_attachments do |f|
=f.input_field :image, multiple: true, name: "progress_attachments_attributes[:image][]"
=f.button :submit
我已经编辑了表单,因为我已经解决了多次上传的问题
模型/ progress_attachment.rb
class ProgressAttachment < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :progress
validates :image, presence: true
end
模型/ progress.rb
class Progress < ActiveRecord::Base
default_scope ->{order(created_at: :DESC)}
mount_uploader :main_image, MainImageUploader
has_many :progress_attachments
accepts_nested_attributes_for :progress_attachments
validates :main_image, presence: true
validates :title, presence: true
validates :content, presence: true
validates :date, presence: true
end
这是我对我实际控制器的撬动。
Started POST "/progresses" for ::1 at 2016-09-02 01:37:49 +0200
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProgressesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KdZkIvo+lbmE8JPlMNOOFc2grNG6w9u4kuO3kII2erOG+iD3RXfqutdwzAxvMIQbVGAzHVGawcBMW+WJfgZ+uA==", "progress"=>{"title"=>"Hello", "date"=>"1st september 2016", "content"=>"bonjourbonjour", "main_image"=>#<ActionDispatch::Http::UploadedFile:0x007fede250ecb8 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22001-1l0mkug.jpg>, @original_filename="12915103_10208497250246588_1486835977_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[main_image]\"; filename=\"12915103_10208497250246588_1486835977_o.jpg\"\r\nContent-Type: image/jpeg\r\n">, "progress_attachments_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fede250eb78 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22001-9k74to.jpg>, @original_filename="band.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[progress_attachments_attributes][0][image]\"; filename=\"band.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Create Progress"}
From: /Users/nelly/Desktop/ROR/leschner_guitars/app/controllers/progresses_controller.rb @ line 20 ProgressesController#create:
19: def create
=> 20: binding.pry
21: @progress = Progress.new(progress_params)
22:
23: respond_to do |format|
24: if @progress.save
25: params[:progress_attachments]['image'].each do |a|
26: @progress_attachment = @progress.progress_attachments.create!(:image => a)
27: end
28:
29: format.html { redirect_to progresses_path, notice: 'Progress was successfully created.' }
30: else
31: format.html { render action: 'new' }
32: end
33: end
34: end
[1] pry(#<ProgressesController>)> params
=> {"utf8"=>"✓",
"authenticity_token"=>"KdZkIvo+lbmE8JPlMNOOFc2grNG6w9u4kuO3kII2erOG+iD3RXfqutdwzAxvMIQbVGAzHVGawcBMW+WJfgZ+uA==",
"progress"=>
{"title"=>"Hello",
"date"=>"1st september 2016",
"content"=>"bonjourbonjour",
"main_image"=>
#<ActionDispatch::Http::UploadedFile:0x007fede250ecb8
@content_type="image/jpeg",
@headers=
"Content-Disposition: form-data; name=\"progress[main_image]\"; filename=\"12915103_10208497250246588_1486835977_o.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="12915103_10208497250246588_1486835977_o.jpg",
@tempfile=#<File:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22001-1l0mkug.jpg>>,
"progress_attachments_attributes"=>
{"0"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x007fede250eb78
@content_type="image/jpeg",
@headers=
"Content-Disposition: form-data; name=\"progress[progress_attachments_attributes][0][image]\"; filename=\"band.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="band.jpg",
@tempfile=#<File:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22001-9k74to.jpg>>}}},
"commit"=>"Create Progress",
"controller"=>"progresses",
"action"=>"create"}
[2] pry(#<ProgressesController>)>
并且像SteveTurczyn一样改变了建议:
Started POST "/progresses" for ::1 at 2016-09-02 01:49:14 +0200
Processing by ProgressesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XevIqKvA6UhcmipGb/zu8wJ3ItCwA5EBjldLQhwyXj7yx4x9FImWSw8ada8wH+T9m7e9HFtai3lQ7xlb4AJaNQ==", "progress"=>{"title"=>"This is a title", "date"=>"Today !", "content"=>"That's a short content", "main_image"=>#<ActionDispatch::Http::UploadedFile:0x007fb00af89dc0 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22410-13jb3ww.jpg>, @original_filename="band.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[main_image]\"; filename=\"band.jpg\"\r\nContent-Type: image/jpeg\r\n">, "progress_attachments_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb00af89c80 @tempfile=#<Tempfile:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22410-6mze6c.jpg>, @original_filename="boss2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"progress[progress_attachments_attributes][0][image]\"; filename=\"boss2.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Create Progress"}
From: /Users/nelly/Desktop/ROR/leschner_guitars/app/controllers/progresses_controller.rb @ line 20 ProgressesController#create:
19: def create
=> 20: binding.pry
21: @progress = Progress.new(progress_params)
22:
23: respond_to do |format|
24: if params[:progress_attachments]
25: params[:progress_attachments]['image'].each do |a|
26: @progress_attachment = @progress.progress_attachments.create!(:image => a)
27: end
28: format.html { redirect_to progresses_path, notice: 'Progress was successfully created.' }
29: else
30: format.html { render action: 'new' }
31: end
32: end
33: end
[1] pry(#<ProgressesController>)> params
=> {"utf8"=>"✓",
"authenticity_token"=>"XevIqKvA6UhcmipGb/zu8wJ3ItCwA5EBjldLQhwyXj7yx4x9FImWSw8ada8wH+T9m7e9HFtai3lQ7xlb4AJaNQ==",
"progress"=>
{"title"=>"This is a title",
"date"=>"Today !",
"content"=>"That's a short content",
"main_image"=>
#<ActionDispatch::Http::UploadedFile:0x007fb00af89dc0
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"progress[main_image]\"; filename=\"band.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="band.jpg",
@tempfile=#<File:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22410-13jb3ww.jpg>>,
"progress_attachments_attributes"=>
{"0"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x007fb00af89c80
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"progress[progress_attachments_attributes][0][image]\"; filename=\"boss2.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="boss2.jpg",
@tempfile=#<File:/var/folders/11/mdddnw8d0zd961bsfkq1cjy00000gn/T/RackMultipart20160902-22410-6mze6c.jpg>>}}},
"commit"=>"Create Progress",
"controller"=>"progresses",
"action"=>"create"}
感谢您的帮助
答案 0 :(得分:4)
unless params[:progress_attachments].nil?
params[:progress_attachments]['image'].each do |a|
@progress_attachment = @progress.progress_attachments.create!(:image => a)
end
end
答案 1 :(得分:3)
这可能是您错误的来源......
params[:progress_attachments]['image'].each do |a|
如果您的参数没有:progress_attachments
键,它将返回nil,并且nil['image']
会正确地显示您所看到的错误。
您可能希望使用create
或byebug
检查进入pry
方法的参数。如果没有进度附件需要存在是合法的,那么你可以将每个循环包装在if params[:progess_attachments] ... end
if params[:progress_attachments]
params[:progress_attachments]['image'].each do |a|
@progress_attachment = @progress.progress_attachments.create!(:image => a)
end
end
我实际上并不清楚这将是完成的解决方案,因为我不清楚你会有一系列图像,但检查参数应该澄清这一点。