简体   繁体   中英

I am getting error undefined method `to_sym' for nil:NilClass in Rails for updating image?

My controller

def update
    handled_error_fields %i(location address1 address2 name name_kanji gender prefecture_code tel zip_code).collect { |s| :"primary_address.#{s}" }

    if params[:salon].present?
      if params[:salon].present?
        if params[:salon][:tel].present?
          tel = params[:staff][:tel]
          params[:staff][:tel] = NKF.nkf('-W -w -m0 -Z0', tel)
        end
      end
      if params[:staff][:email].present?
        email = params[:staff][:email]
        email.gsub!(/。/, '.')
        params[:staff][:email] = NKF.nkf('-W -w -m0 -Z0', email)
      end
    end

    if params[:staff]["staff_image"].present?
      @staff_image = StaffImage.find_by_staff_id(current_staff.id)
       if @staff_image.blank?
        @staff_image = StaffImage.new 
        @staff_image.staff_id = current_staff.id
        @staff_image.legacy_user_id = current_staff.legacy_user_id    
        @staff_image.image = params[:staff]["staff_image"]["image"].read
        @staff_image.save!
      else

        @staff_image.image = params[:staff]["staff_image"]["image"].read
        @staff_image.update_attribute('image', @staff_image.image)
       end
    end
    super
  end

My Model is

class StaffImage < ActiveRecord::Base

end

it has columns staff_id , image , legacy_client_id .

Real problem is after insertion of image in database, I can't update the image. I am new to Rails. I know problem is in this line:

@staff_image.update_attribute('image', @staff_image.image)

Not using any attachment plugins? Have a look on https://github.com/thoughtbot/paperclip gem. Way too easy for handle this kind of things in Rails.

Not necessary to use update_attribute method. Simple save could solve.

@staff_image.image = params[:staff]["staff_image"]["image"].read 
@staff_image.save

You might want to try calling "with_indifferent_access" like:

params = params.with_indifferent_access

before accessing "staff_image"

params[:staff]["staff_image"]["image"]

this should get rid of the error and then:

@staff_image.image = params[:staff]["staff_image"]["image"].read 
@staff_image.save

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM