일련의 사용자를 업로드하기 위해 csv 파일로 이동하는 file_field 옵션이 있습니다. 현재 파일 추가없이 사용자 업로드 버튼을 클릭 할 수 있습니다. 나는이 nil 예외를 잡았는지 확인하려고하지만 어떻게 할 수 없는가하는 생각이 듭니다. 내 컨트롤러를 변경하거나 버튼을 비활성화하여 양식을 변경해야합니다 어떻게 든제출 버튼 비활성화 file_field
나는 파일 업로드는 아래의 간단한 양식이 있습니다
def upload
if request.post?
if params[:tenant][:csv].blank?
flash[:notice] = "Please provide a csv file to upload."
else
file = params[:tenant][:csv].read
CSV.parse(file, headers: true, header_converters: :symbol).each { |row| User.create(row.to_hash) }
end
respond_to do |format|
format.html { redirect_to users_path }
format.json { head :no_content }
end
else
# Return view
end
end
: 나는 컨트롤러에 다음과 다음과 같은 전망을
<%= simple_form_for :tenant, :html => {:multipart => true}, :url => users_path do |f| %>
<%= f.file_field :csv, :label => 'CSV File' %>
<%= f.submit 'Upload Users' %>
<% end %>
을