Rails& Carrierewave:belongs_to模型保存图像(硬编码)字符串为NULL?

时间:2016-11-26 22:55:29

标签: ruby-on-rails image carrierwave

没有意义,硬编码字符串如何在服务器上变为NULL?我尝试用不同的方法保存它,都具有相同的结果。 我正在使用nrierx,mysql,react和rails 5与Carrierwave运行docker。

create_table "galleries", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
  t.string   "name"
  t.text     "description", limit: 65535
  t.datetime "created_at",                null: false
  t.datetime "updated_at",                null: false
end

create_table "gallery_images", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
  t.integer  "gallery_id"
  t.string   "image"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false

class ImageForm extends React.Component {
    return (
  <form class="new_gallery" id="new_gallery" enctype="multipart/form-data" action="/galleries" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="DCMwHHjARRa1p+tCf8dg2+SIq3Jp8yEAhZtmuqc8nOy0auZdY/pxlTlp1rla0AzL5z3pzsUOZpkfcYWqo/EK5g==" />
    <div class="field">
      <label for="gallery_name">Navn</label>
      <input type="text" name="gallery[name]" id="gallery_name" />
    </div>
    <div class="field">
      <label for="gallery_description">Beskerivelse</label>
      <textarea name="gallery[description]" id="gallery_description"></textarea>
    </div>
      <div class="field">
        <label for="gallery_gallery_images_attributes_0_image">Billeder</label>
        <input multiple="multiple" name="gallery_images[image][]" type="file" id="gallery_gallery_images_attributes_0_image" />
      </div>
    <div class="actions">
      <input type="submit" name="commit" value="Create Gallery" data-disable-with="Create Gallery" />
    </div>
  </form>
  )}
};

class Gallery < ApplicationRecord
  has_many :gallery_images
  accepts_nested_attributes_for :gallery_images
end

class GalleryImage < ApplicationRecord
  mount_uploader :image, ImageUploader
  belongs_to :gallery
end

# POST /galleries
def create
  @gallery = Gallery.new(gallery_params)
  logger.debug(params[:gallery_images]['image'][0])    
  if @gallery.save
    @gallery_images = GalleryImage.create!(gallery_id: @gallery.id, image: "5_star.png")     
     redirect_to @gallery, notice: 'Gallery was successfully created.'
   else
  render :new
end
def gallery_params
  params.require(:gallery).permit(:name, :description, gallery_images_attributes: [:id, :gallery_id, :image])
end

和日志输出

Started POST "/galleries" for 127.0.0.1 at 2016-11-25 23:31:46 +0000
Processing by GalleriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"14bAJ6+hQU330FW2xIpP2Cy8NGKzVF8GtJQreqO0mIRIcMWNT1V1L+m9cSfH3dpoZhc/alOtjlQnGTGh/gmTaQ==", 
"gallery"=>{"name"=>"jkl", "description"=>"jkl"}, "gallery_images"=>  {"image"=>["1_star.png"]}, "commit"=>"Create Gallery"}
1_star.png // <---- DEBUG
(0.2ms)  BEGIN
SQL (0.4ms)  INSERT INTO `galleries` (`name`, `description`,     `created_at`, `updated_at`) 
VALUES ('jkl', 'jkl', '2016-11-25 23:31:46',   '2016-11-25 23:31:46')
(74.6ms)  COMMIT
(0.4ms)  BEGIN
Gallery Load (0.4ms)  SELECT  `galleries`.* FROM `galleries` WHERE    `galleries`.`id` = 96 LIMIT 1
SQL (0.4ms)  INSERT INTO `gallery_images` (`gallery_id`, `created_at`, `updated_at`, `image`) 
VALUES (96, '2016-11-25 23:31:46', '2016-11-25 23:31:46', NULL) // <---- NULL???
(81.9ms)  COMMIT
Redirected to http://0.0.0.0:8080/galleries/96
Completed 302 Found in 209ms (ActiveRecord: 161.9ms)

nginx_1   | 172.18.0.1 - - [25/Nov/2016:23:31:23 +0000] "POST /galleries HTTP/1.1" 302 109 "http://0.0.0.0:8080/galleries/new" 

白发以上..有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我通过将formelements更改为camelcase来解决它,例如。 enctype - &gt; ENCTYPE accept-charset - &gt; acceptCharset。 仍然不能幻想它是如何能够改变第二个它的价值......