Rails Carrierwave 上传多个文件 nil:NilClass 的未定义方法“[]”
Rails Carrierwave upload multiple files undefined method `[]' for nil:NilClass
谁能解释一下为什么会这样
undefined method `[]' for nil:NilClass
在使用 rails、载波和反应上传文件时发生?
其余代码非常标准,因此在上传图片的日志中,post_attachments 中的图片属性始终显示为空白。
我怀疑问题出在 post 创建的 post_attachments 上的 rails 循环,你们怎么看?
我改成了
params[:post_attachments].each do |image|
img = PostAttachment.new
img.image = image
@post.post_attachments << img
end
仍然显示错误。
所以,有人知道为什么这与载波有反应吗?
这里是post控制器
def create
@post = current_user.posts.build(post_params)
if @post.save
#unless params[:post_attachments].nil?
params[:post_attachments]['image'].each do |a|
@post_attachment = @post.post_attachments.create!(:image => a, :post_id => @post.id)
#end
end
render json: "Posted successfully", status: 201
else
render json: @post.errors, status: :unprocessable_entity
end
end
和反应
async sendImage(forma) {
let self = this;
let errs = 0;
let auth_token = await AsyncStorage.getItem(AUTH_TOKEN);
try {
let response = await fetch("https://localhost:3000/posts", {
method: "POST",
headers: {
Accept: "application/json",
//"Content-Type": "multipart/form-data",
Access: auth_token
},
body: forma
});
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
} else {
//Handle errors
let error = res;
throw error;
}
} catch (errors) {
//errors are in JSON form so we must parse them first.
let formErrors = JSON.parse(errors);
//We will store all the errors in the array.
let errorsArray = [];
for (var key in formErrors) {
//If array is bigger than one we need to split it.
if (formErrors[key].length > 1) {
formErrors[key].map(error => errorsArray.push(`${key} ${error}`));
} else {
errorsArray.push(`${key} ${formErrors[key]}`);
}
}
this.setState({ errors: errorsArray });
//this.setState({showProgress: false});
}
}
async uploadImage() {
const { params } = this.props.navigation.state;
let image = params ? params.image : null;
let photos = params ? params.photos : [];
var self = this;
let forma = new FormData();
forma.append("post[title]", String(this.state.title));
forma.append("post[body]", String(this.state.body));
forma.append("post[post_attachments][image][]", 'teste');
for (let i = 0; i < photos.length; ++i) {
await fs
.readFile(photos[i], "base64")
.then(async blob => {
//forma.append("post[post_attachments][image][]", 'teste');
.catch(error => {
console.log("loop err", error);
});
}
self.sendImage(forma);
}
日志截图
参数是这样的:-
paramaters: {"post" = > .... "post_attachments" => {"image" => ["test"]}}
所以image_params应该是这样的-
image_params = params[:post][:post_attachments][:image]
和
unless image_params.blank?
image_params.each do |image|
img = PostAttachment.new
img.image = image
@post.post_attachments << img
end
end
谁能解释一下为什么会这样
undefined method `[]' for nil:NilClass
在使用 rails、载波和反应上传文件时发生?
其余代码非常标准,因此在上传图片的日志中,post_attachments 中的图片属性始终显示为空白。
我怀疑问题出在 post 创建的 post_attachments 上的 rails 循环,你们怎么看?
我改成了
params[:post_attachments].each do |image|
img = PostAttachment.new
img.image = image
@post.post_attachments << img
end
仍然显示错误。
所以,有人知道为什么这与载波有反应吗?
这里是post控制器
def create
@post = current_user.posts.build(post_params)
if @post.save
#unless params[:post_attachments].nil?
params[:post_attachments]['image'].each do |a|
@post_attachment = @post.post_attachments.create!(:image => a, :post_id => @post.id)
#end
end
render json: "Posted successfully", status: 201
else
render json: @post.errors, status: :unprocessable_entity
end
end
和反应
async sendImage(forma) {
let self = this;
let errs = 0;
let auth_token = await AsyncStorage.getItem(AUTH_TOKEN);
try {
let response = await fetch("https://localhost:3000/posts", {
method: "POST",
headers: {
Accept: "application/json",
//"Content-Type": "multipart/form-data",
Access: auth_token
},
body: forma
});
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
} else {
//Handle errors
let error = res;
throw error;
}
} catch (errors) {
//errors are in JSON form so we must parse them first.
let formErrors = JSON.parse(errors);
//We will store all the errors in the array.
let errorsArray = [];
for (var key in formErrors) {
//If array is bigger than one we need to split it.
if (formErrors[key].length > 1) {
formErrors[key].map(error => errorsArray.push(`${key} ${error}`));
} else {
errorsArray.push(`${key} ${formErrors[key]}`);
}
}
this.setState({ errors: errorsArray });
//this.setState({showProgress: false});
}
}
async uploadImage() {
const { params } = this.props.navigation.state;
let image = params ? params.image : null;
let photos = params ? params.photos : [];
var self = this;
let forma = new FormData();
forma.append("post[title]", String(this.state.title));
forma.append("post[body]", String(this.state.body));
forma.append("post[post_attachments][image][]", 'teste');
for (let i = 0; i < photos.length; ++i) {
await fs
.readFile(photos[i], "base64")
.then(async blob => {
//forma.append("post[post_attachments][image][]", 'teste');
.catch(error => {
console.log("loop err", error);
});
}
self.sendImage(forma);
}
日志截图
参数是这样的:-
paramaters: {"post" = > .... "post_attachments" => {"image" => ["test"]}}
所以image_params应该是这样的-
image_params = params[:post][:post_attachments][:image]
和
unless image_params.blank?
image_params.each do |image|
img = PostAttachment.new
img.image = image
@post.post_attachments << img
end
end