Ruby版本2.2.4 Rails版本5.0.0.1 大家好,
我编写了一个Android应用程序,可以从用户那里获取个人资料图片。现在我想将这些个人资料图片上传到我的Ruby on Rail服务器。但是上传不起作用。我收到错误消息:
Response from Servers org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.3.11:3000 refused
这是我在android-app中使用的主机地址。我使用我的本地ip-adress所以这部分应该是正确的。
public class Config {
// File upload url (replace the ip with your server address)
// public static final String FILE_UPLOAD_URL = "http://192.168.3.11:3000/AndroidFileUpload/fileUpload.php";
public static final String FILE_UPLOAD_URL = "http://192.168.3.11:3000/";
// Directory name to store captured images and videos
public static final String IMAGE_DIRECTORY_NAME = "android_upload";
}
这是我在rails应用程序上的ruby: 的 items_controller
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
# GET /items
# GET /items.json
def index
@items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
send_data(item.file_contents,
type: @item.content_type,
filename: @item.filename)
end
# POST /items
# POST /items.json
def create
@item = Item.new(item_params)
if @item.save
render :show, status: :created, location: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
if @item.update(item_params)
render :show, status: :ok, location: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:name, :description, :picture)
end
end
模型/ item.rb的
class Item < ApplicationRecord
mount_uploader :picture, PictureUploader
end
上传/ picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
的routes.rb
Rails.application.routes.draw do
resources :items
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
# Serve websocket cable requests in-process
# mount ActionCable.server => '/cable'
end
更新:我在routes.db中添加了一个帖子:post to:'items'并使用rails s -b 192.168.3.11启动RoR服务器。现在我得到以下错误,
app/controllers/items_controller.rb:55:in `item_params'
app/controllers/items_controller.rb:21:in `create'
Started POST "/items" for 192.168.3.7 at 2016-09-11 01:12:21 +0900
Processing by ItemsController#create as HTML
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x5737110 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-2
8624-1vjbftr.jpg>, @original_filename="IMG_20160911_010525.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; na
me=\"image\"; filename=\"IMG_20160911_010525.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: item):
app/controllers/items_controller.rb:55:in `item_params'
app/controllers/items_controller.rb:21:in `create'
在此错误消息之后,我将参数输出了但仍然是相同的错误:
def item_params
#######################params.require(:item).permit(:name, :description, :picture)
params.permit(:picture)