기사를 작성하기 위해 양식을 작성했으며 제목, 날짜, 내용, 기본 이미지 필드와 다중 업로드를위한 특수 필드가 있습니다. 이미지는 Carrierwave로 업로드됩니다.
이 양식에서 작동하는 두 가지 다른 모델과 컨트롤러가 있습니다. progress 및 progress_attachments .
사진 갤러리를 만들기 위해 여러 이미지를 첨부하고 싶기 때문에 다른 컨트롤러를 만들었습니다 progress_attachment SSR 설명을 따랐습니다
양식을 제출할 때이 오류가 발생합니다.
NoMethodError in ProgressesController#create
undefined method `[]' for nil:NilClass
기사가 게시되었지만 ... (레일스 콘솔에서 찾을 수 있으며 쇼로 돌아 가면)
나는 이것이 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 / progresses / 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
여러 업로드 문제를 해결했기 때문에 양식을 편집했습니다.
models / progress_attachment.rb
class ProgressAttachment < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :progress
validates :image, presence: true
end
models / 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"}
당신의 도움을 주셔서 감사합니다
unless params[:progress_attachments].nil?
params[:progress_attachments]['image'].each do |a|
@progress_attachment = @progress.progress_attachments.create!(:image => a)
end
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다