i have created form in order create articles, has fields title, date, content, main image, , special field multiple upload. images uploaded carrierwave.
i have 2 differents models , controllers work form. progress , progress_attachments.
as want attach multiple images in order create photos gallery have created other controller progress_attachment i followed ssr explanations
when submit form, have error
nomethoderror in progressescontroller#create undefined method `[]' nil:nilclass
even though article published... (i can find in rails console, , if go show)
i beleive undefined method []
comes create method in the progress controller, missing , don't understand what...??
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 |format| if @progress.save params[:progress_attachments]['image'].each |a| @progress_attachment = @progress.progress_attachments.create!(:image => a) end format.html { redirect_to progresses_path, notice: 'progress created.' } else format.html { render action: 'new' } end end end def update respond_to |format| if @progress.update(article_params) format.html { redirect_to @progress, notice: 'article 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 |format| format.html { redirect_to articles_url, notice: 'article 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 |format| if @progress_attachment.save format.html { redirect_to @progress_attachment, notice: 'progress attachment 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 |format| if @progress_attachment.update(progress_attachment_params) format.html { redirect_to @progress_attachment, notice: 'progress attachment 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 |format| format.html { redirect_to progress_attachments_url, notice: 'progress attachment 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
the form in views/progresses/new.html.slim
= simple_form_for(@progress, html: { multipart: true} ) |f| =f.input :title =f.input :date =f.input :content =f.input :main_image =f.simple_fields_for :progress_attachments |f| =f.input_field :image, multiple: true, name: "progress_attachments_attributes[:image][]" =f.button :submit
i have edited form since have solved problem of multiple uploads
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
edit
this pry on actual controller.
started post "/progresses" ::1 @ 2016-09-02 01:37:49 +0200 activerecord::schemamigration load (0.1ms) select "schema_migrations".* "schema_migrations" processing progressescontroller#create 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 |format| 24: if @progress.save 25: params[:progress_attachments]['image'].each |a| 26: @progress_attachment = @progress.progress_attachments.create!(:image => a) 27: end 28: 29: format.html { redirect_to progresses_path, notice: 'progress 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>)>
and changing steveturczyn suggested:
started post "/progresses" ::1 @ 2016-09-02 01:49:14 +0200 processing progressescontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"xeviqkva6uhcmipgb/zu8wj3itcwa5ebjldlqhwyxj7yx4x9fimwsw8ada8wh+t9m7e9hftai3lq7xlb4ajanq==", "progress"=>{"title"=>"this title", "date"=>"today !", "content"=>"that's 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 |format| 24: if params[:progress_attachments] 25: params[:progress_attachments]['image'].each |a| 26: @progress_attachment = @progress.progress_attachments.create!(:image => a) 27: end 28: format.html { redirect_to progresses_path, notice: 'progress 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 title", "date"=>"today !", "content"=>"that's 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"}
thanks help
unless params[:progress_attachments].nil? params[:progress_attachments]['image'].each |a| @progress_attachment = @progress.progress_attachments.create!(:image => a) end end
Comments
Post a Comment