Я загрузив файл JSON в Rails для того, чтобы разобрать его, а затем вставить в базу данных без сохранения самого файлаRead загруженный файл JSON в Rails
Форма
= form_tag import_posts_path do
= file_field_tag :file
= submit_tag "import"
метод контроллера
def import
file = params[:file]
puts "file => " + file # file => posts.json
data = File.read(params[:file]) # Errno::ENOENT at /posts/import No such file or directory @ rb_sysopen - posts.json
json = JSON.parse(data)
end
маршруты
resources :posts do
collection { post :import }
end
Информации запроса в терминале, и posts.json
есть, но я получаю Errno::ENOENT at /posts/import No such file or directory @ rb_sysopen - posts.json
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9lzpwyCWclgiI4WA/zho5y1WLCBQtRL7z3FcAn+wc80=", "file"=>"posts.json", "commit"=>"import"}
file => posts.json
Completed 500 Internal Server Error in 23ms
Вы должны отправить его как данные в виде многостраничной формы – usmanali
Посмотрите, подходит ли эта нить http://stackoverflow.com/questions/2521053/how-to-read-a-user-uploaded-file-without-saving -it-to-the-database – xenover
Спасибо @usmanali, что сделал трюк плюс новая работа в методе –