【问题标题】:Rails 4 multiple image uploading using carrierwaveRails 4 使用carrierwave上传多张图片
【发布时间】:2017-03-02 03:44:06
【问题描述】:

如何使用 CarrierWave 上传多张图片?

我关注了来自

的“drjorgepolanco”回答

Rails 4 multiple image or file upload using carrierwave

但我不断收到错误

    undefined method `[]' for #<AssignmentpicUploader:0x007eff7831a4d8>
    Extracted source (around line #25):

    ActionView::Template::Error (undefined method `[]' for #<AssignmentpicUploader:0x007eff7831a4d8>):
    22:    
    23:    
    24:    <div id="img">
    25:     <%= image_tag @assignment.picture[1].url(:large) %>
    26:   </div>
    27:   
    28:   </br>
  app/views/assignments/show.html.erb:25

assignment.rb

class Assignment < ActiveRecord::Base
        validates :name, length: { minimum: 1, maximum:120 }
        validates :description, length: { minimum: 1 }
        mount_uploader :picture, AssignmentpicUploader
end

assignments_controller.rb

class AssignmentsController < ApplicationController
  before_action :authenticate_user!
  before_filter :admin_access, only: [:new, :create, :edit, :update, :destroy]


    def index
     @assignments = Assignment.all.order("created_at DESC")
    end


   def show
     @assignment = Assignment.find(params[:id])
   end

   def new
     @assignment = Assignment.new
   end  

   def create
     @assignment = current_user.assignments.build
     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]


     if @assignment.save
       flash[:notice] = "Assignment was saved successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error creating assignment. Please make sure there is a name and description."
       render :new
     end
   end

     def edit
     @assignment = Assignment.find(params[:id])
     end

      def update
     @assignment = Assignment.find(params[:id])

     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]

     if @assignment.save
        flash[:notice] = "Assignment was updated successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error saving assignment. Please try again."
       render :edit
     end
      end

   def destroy
     @assignment = Assignment.find(params[:id])

     if @assignment.destroy
       flash[:notice] = "\"#{@assignment.name}\" was deleted successfully."
       redirect_to action: :index
     else
       flash.now[:alert] = "There was an error deleting the assignment."
       render :show
     end
   end

     private 

     def assignment_params
         params.require(:assignment).permit(:name, :description, picture: [])
     end
end

assignments/new.html.erb

        ...
       <div class="col-md-8">
         <%= form_for @assignment, :html => { :multipart => true } do |f| %>
           <div class="form-group">
             <%= f.label :name %>
             <%= f.text_field :name, class: 'form-control', placeholder: "Enter assignment name" %>
           </div>
           <div class="form-group">
             <%= f.label :description %>
             <%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter assignment description" %>
           </div>
           <div class="form-group">
          <%= f.label :picture %>
          <%= f.file_field :picture, :multiple => true %>
          </div>
          <br />
           <%= f.submit "Save", class: 'button' %>
         <% end %>
       </div>
     </div>

assignments/show.html.erb

    <%= image_tag @assignment.picture[1].url(:large) %>

schema.rb

create_table "assignments", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.string   "picture"
  end

  add_index "assignments", ["user_id"], name: "index_assignments_on_user_id"

【问题讨论】:

  • 在创建动作中flash.now[:alert]... 前设置断点,查看@assignment.errors的结果。有人只能为您提供有关错误的详细信息
  • 如何设置断点?对不起,我还是个菜鸟。我需要调试器 gem 来执行此操作吗?
  • 你会的。我建议使用github.com/pry/pry

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave image-uploading


【解决方案1】:

Carrierwave 文档说:

添加一个可以存储数组的列。例如,这可以是数组列或 JSON 列。您的选择取决于您的数据库支持什么。

您的“图片”只是一个字符串。将serialize :picture, JSON 添加到您的模型中。

【讨论】:

  • 感谢您停止验证错误消息并将我指向导致错误的代码,我现在已经提出了我的问题。
  • 正如您在错误中看到的那样,问题在于无法存储为字符串的数组。请按照我的回答提出建议。
  • 我已将“序列化:图片,JSON”添加到我的 assignment.rb 文件中,但如何将 :picture 字符串更改为 :JSON 列类型?
  • 没有这种列类型“json”。您将数组转换为 json 对象并将其保存在“字符串”列中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多