CarrierWaveを使用して複数の画像をアップロードするにはどうすればよいですか?Rails 4 carrierwaveを使用した複数の画像のアップロード
は私が
Rails 4 multiple image or file upload using carrierwave
から "drjorgepolanco" の答えを追ったが、私はエラー
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
の割り当て/ 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>
の割り当て/ 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'''の結果を確認してください。誰かがエラー – Zelenka
についての詳細な情報だけを手伝ってくれます。どのようにブレークポイントを設定しますか?申し訳ありません私はまだnoobです。これを行うにはデバッガの宝石が必要ですか? –
です。私はhttps://github.com/pry/pryを使用することをお勧めします – Zelenka