载波不保存图像
carrierwave not saving image
我正在使用 Angularjs 作为前端,
我可以上传图片和一些参数。
我正在使用此指令 angular-file-upload 上传带有参数的单个图像:
uploader = $scope.uploader = new FileUploader(
url: '/recipes',
alias: 'cover',
removeAfterUpload: true,
#transformRequest: angular.identity,
headers: {'X-CSRF-TOKEN': csrf_token,'accept': 'application/json'},
withCredentials: true
)
uploader.onBeforeUploadItem = (item)->
#data = angular.toJSON($scope.recipe)
item.formData.push("recipe": angular.toJson($scope.recipe))
#item.upload()
console.info('uploader', $scope.uploader);
uploader.uploadAll()
这是创建操作:
def create
params[:recipe] = JSON.parse params[:recipe]
params[:recipe][:cover] = params[:cover]
@ingredients = Ingredient.where(:id => params[:recipe][:ingredients].map {|ingredient| ingredient[:id]})
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name,:instructions))
@recipe.ingredients << @ingredients
@recipe.user_id = current_user.id
@recipe.save
render 'show', status: 201
end
配方模型:
class Recipe < ActiveRecord::Base
mount_uploader :cover, AvatarUploader
belongs_to :user
has_and_belongs_to_many :ingredients,:join_table => "ingredients_recipes"
accepts_nested_attributes_for :ingredients
attr_accessor :cover
end
这是请求:
-----------------------------100101598926016265538511946
Content-Disposition: form-data; name="recipe"
{"name":"amzpld","instructions":"mpzmdpzmez","ingredients":[{"id":3,"title":"oeuf"}]}
-----------------------------100101598926016265538511946
Content-Disposition: form-data; name="cover"; filename="10.jpg"
Content-Type: image/jpeg
ÿØÿà�JFIF������ÿÛ�� ( %"1"%),...383,7(-.+
0& $,,,2,,,,,,4,4,,,,,,,,,-,,,,,,,,,,,4,,,,,,,,,,,,,,,ÿÀ��¨,"�ÿÄ�������������
�ÿÄ�@�����!1AQa"q2¡±#BRÁáð3bÑr$ñCT¢ÿÄ��������������
ÿÄ�0�������!1A"Qaq¡2±#B3CÑáðÿÚ���?�öá]®Qâ1 Bs(À¡V_D$¯8þ&¡i6¨Û)Ò
´oóçÊ�dÌôªçñEÏêÚÙ]ëãzn£hÓÚêÄ6I½&&·¼ßËðȨ̈)L¤hGQÞ°Pf�MÔúSkÔBønàôÕjal3YðäKÇÖ¾Á¢@..........
食谱已保存,但封面 = nil
请问我缺少什么
您正在使 :cover
成为您的 params[:recipe]
散列的一部分:
params[:recipe][:cover] = params[:cover]
但是你没有将它列入白名单,所以不是:
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name,:instructions))
试试这个:
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name, :instructions, :cover))
更新
您的 Recipe
模型还有另一个问题,您使用的是:
attr_accessor :cover
删除该行,你不需要它,因为 cover
应该是你的 recipes
table 中的一个真实列,如果该列已经存在,这将覆盖该方法为该列分配值,从而导致您的列包含 nil
.
现在,如果您的 recipes
table 中没有该列,只需使用迁移添加它,在您的控制台类型中:
rails g migration add_cover_to_recipes cover:string
然后迁移:
rake db:migrate
希望能解决您的问题。
我正在使用 Angularjs 作为前端, 我可以上传图片和一些参数。 我正在使用此指令 angular-file-upload 上传带有参数的单个图像:
uploader = $scope.uploader = new FileUploader(
url: '/recipes',
alias: 'cover',
removeAfterUpload: true,
#transformRequest: angular.identity,
headers: {'X-CSRF-TOKEN': csrf_token,'accept': 'application/json'},
withCredentials: true
)
uploader.onBeforeUploadItem = (item)->
#data = angular.toJSON($scope.recipe)
item.formData.push("recipe": angular.toJson($scope.recipe))
#item.upload()
console.info('uploader', $scope.uploader);
uploader.uploadAll()
这是创建操作:
def create
params[:recipe] = JSON.parse params[:recipe]
params[:recipe][:cover] = params[:cover]
@ingredients = Ingredient.where(:id => params[:recipe][:ingredients].map {|ingredient| ingredient[:id]})
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name,:instructions))
@recipe.ingredients << @ingredients
@recipe.user_id = current_user.id
@recipe.save
render 'show', status: 201
end
配方模型:
class Recipe < ActiveRecord::Base
mount_uploader :cover, AvatarUploader
belongs_to :user
has_and_belongs_to_many :ingredients,:join_table => "ingredients_recipes"
accepts_nested_attributes_for :ingredients
attr_accessor :cover
end
这是请求:
-----------------------------100101598926016265538511946
Content-Disposition: form-data; name="recipe"
{"name":"amzpld","instructions":"mpzmdpzmez","ingredients":[{"id":3,"title":"oeuf"}]}
-----------------------------100101598926016265538511946
Content-Disposition: form-data; name="cover"; filename="10.jpg"
Content-Type: image/jpeg
ÿØÿà�JFIF������ÿÛ�� ( %"1"%),...383,7(-.+
0& $,,,2,,,,,,4,4,,,,,,,,,-,,,,,,,,,,,4,,,,,,,,,,,,,,,ÿÀ��¨,"�ÿÄ�������������
�ÿÄ�@�����!1AQa"q2¡±#BRÁáð3bÑr$ñCT¢ÿÄ��������������
ÿÄ�0�������!1A"Qaq¡2±#B3CÑáðÿÚ���?�öá]®Qâ1 Bs(À¡V_D$¯8þ&¡i6¨Û)Ò
´oóçÊ�dÌôªçñEÏêÚÙ]ëãzn£hÓÚêÄ6I½&&·¼ßËðȨ̈)L¤hGQÞ°Pf�MÔúSkÔBønàôÕjal3YðäKÇÖ¾Á¢@..........
食谱已保存,但封面 = nil 请问我缺少什么
您正在使 :cover
成为您的 params[:recipe]
散列的一部分:
params[:recipe][:cover] = params[:cover]
但是你没有将它列入白名单,所以不是:
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name,:instructions))
试试这个:
@recipe = current_user.recipes.new(params.require(:recipe).permit(:name, :instructions, :cover))
更新
您的 Recipe
模型还有另一个问题,您使用的是:
attr_accessor :cover
删除该行,你不需要它,因为 cover
应该是你的 recipes
table 中的一个真实列,如果该列已经存在,这将覆盖该方法为该列分配值,从而导致您的列包含 nil
.
现在,如果您的 recipes
table 中没有该列,只需使用迁移添加它,在您的控制台类型中:
rails g migration add_cover_to_recipes cover:string
然后迁移:
rake db:migrate
希望能解决您的问题。