2016-09-01 1 views
1

Я создал форму для создания статей, у нее есть поля для названия, даты, содержимого, основного изображения и специального поля для множественной загрузки , Изображения загружаются с помощью Carrierwave.undefined method `[] 'для nil: NilClass, но статья опубликована

У меня есть две разные модели и контроллеры, которые работают с этой формой. прогресс и progress_attachments.

Как я хочу, чтобы прикрепить несколько изображений, чтобы создать фото галерею Я создал этот другой контроллер progress_attachmentЯ последовал SSR explanations

Когда я представить мою форму, я эту ошибку

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 

форма в представлениях/прогрессирует/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 

EDIT

это мой подлец на моем фактическом контроллере.

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"} 

Спасибо за вашу помощь

ответ

4
unless params[:progress_attachments].nil? 
    params[:progress_attachments]['image'].each do |a| 
    @progress_attachment = @progress.progress_attachments.create!(:image => a) 
    end 
end 
+0

Спасибо большое! это работает лучше !!! –

3

Это вероятный источник вашей ошибки ...

params[:progress_attachments]['image'].each do |a| 

Если ваш Params нет ключа :progress_attachments, он вернет ноль, а 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 

Я на самом деле не ясно, что это будет готовое решение, так как я не ясно, что вы будете иметь массив изображений, но проверка параметров должна прояснить это.

+0

Привет, Steve спасибо за ваш ответ, я действительно не знаю, как использовать pry или beebug, не могли бы вы объяснить мне, пожалуйста? Я новичок ... –

+1

Установите gem pry в свой Gemfile, вставив три строки ... 'group: development, test do; драгоценный камень; end ", затем выполните' bundle install' для установки pry. Затем сразу же после того, как ваша строка 'def create' помещается в строку' binding.pry', и в следующий раз, когда метод create вызывается в процессе разработки, консоль остановится, и вы можете вводить команды, такие как 'params', чтобы увидеть, что содержат параметры , Когда вы будете готовы возобновить свое приложение, введите 'exit', и приложение будет продолжено. – SteveTurczyn

+0

не могли бы вы помочь мне немного больше, пожалуйста. Я обновил вопрос с pry :) –