Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Carrier未保存图像_Ruby On Rails_Angularjs_Carrierwave - Fatal编程技术网

Ruby on rails Carrier未保存图像

Ruby on rails Carrier未保存图像,ruby-on-rails,angularjs,carrierwave,Ruby On Rails,Angularjs,Carrierwave,我使用Angularjs作为前端, 我可以上传图像和一些参数。 我使用此指令angular file upload来上载带有参数的单个图像: uploader = $scope.uploader = new FileUploader( url: '/recipes', alias: 'cover', removeAfterUpload: true, #transformRequest: angular.identity, headers: {'X-CSRF-TOKEN':

我使用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
配方已保存,但封面=无
请告诉我我遗漏了什么

您正在制作的
:覆盖
参数的一部分[: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))
更新 您的
配方
模型中还有另一个问题,您正在使用:

attr_accessor :cover
删除该行,您不需要它,因为
cover
应该是
recipes
表中的真实列,如果该列已经存在,则这将覆盖为该列赋值的方法,从而导致列包含
nil

现在,如果您的
recipes
表中没有该列,只需使用迁移将其添加到控制台类型中:

rails g migration add_cover_to_recipes cover:string 
然后迁移:

rake db:migrate

希望这能解决您的问题。

您正在制作
:覆盖
参数的一部分[: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))
更新 您的
配方
模型中还有另一个问题,您正在使用:

attr_accessor :cover
删除该行,您不需要它,因为
cover
应该是
recipes
表中的真实列,如果该列已经存在,则这将覆盖为该列赋值的方法,从而导致列包含
nil

现在,如果您的
recipes
表中没有该列,只需使用迁移将其添加到控制台类型中:

rails g migration add_cover_to_recipes cover:string 
然后迁移:

rake db:migrate

希望能解决你的问题。

cover
仍然是
nil
?是的:
[{“id”:120,“name”:“mspam”,“instructions”:“losap”,“cover”:null,“created_at”:“2015-12-08T22:34:00.124Z”,“components”:[{“id”:2,“title”:“oeuf”,“created_at”:“2015-11-24T16:20:16.454Z”,“updated_at”:“2015-11-24T16:20:16.454Z”;“user”}id],“user”]
请在
创建
操作开始时使用
byebug
,当您获得调试控制台时,检查
:cover
内容:
放置参数[:cover]
放置参数[:cover]\nil
@SalemHarrathi嘿,我刚刚注意到您在模型上使用了“attr\u accessor:cover”。你为什么这么做
code
应该是你的
食谱表中真正的一列。
code
仍然是
nil
?是的:
[{“id”:120,“名称”:“mspam”,“说明”:“losap”,“code”:null,“创建时间”:“2015-12-08T22:34:00.124Z”,“成分”:[{“id”:2,“标题”:“oeuf”,“创建时间”:“2015-11-24T16:20:16.454Z”,“更新时间”:“2015-11-24T16:20:16.454Z”},“user_id:1}]
请在
创建
操作开始时将
byebug
,当您得到调试控制台检查
:cover
内容:
放置参数[:cover]
放置参数[:cover]\nil
@SalemHarrathi嘿,我刚刚注意到您正在使用”attr_accessor:cover“在您的模型上。您为什么要这样做?
cover
应该是您的
配方表中的真实列。