保存多个值代入列 [英] Save multiple values into column

查看:115
本文介绍了保存多个值代入列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储从JSONP请求检索到我的Rails数据库的多个值,所以我可以在以后显示出不同的结果。

I'm trying to store multiple values retrieved from a JSONP request into my rails database so I can show the different results later.

我已经创建了一个功能,在我的控制器这样的,

I've created a function in my controller like this,

movieAdd.trailer(movie.id)
  .then(function(response){
    $scope.youtubeTrailer = response;
    console.log ($scope.youtubeTrailer)

这要求预告片服务于我的movieService.js,

This requests the trailer service in my movieService.js,

var service = {};
var baseUrl = 'http://api.themoviedb.org/3/movie/';
function httpPromise (url) {
  var deferred = $q.defer();
  $http({
    method:'JSONP',
    url: url
  })
    .success(function(data){
      deferred.resolve(data);
    })
    .error(function(){
      deferred.reject();
    });
  return deferred.promise;
}
service.trailer = function(youtube_link){
  return httpPromise(
    baseUrl + youtube_link + '/videos?api_key=a8f7039633f2065942c**a28d7cadad4&callback=JSON_CALLBACK'
  )
};

和我存储在 youtubeTrailer 范围的结果。

And I store the results in the youtubeTrailer scope.

然后,我有一个创建功能,

Then I have a create function,

createMovie.create({
  release_date:   $scope.movieListID.release_date,
  youtube_link:   $scope.youtubeTrailer.key,
  imdb_rating:    $scope.movieImdbRating.imdbRating,
  title:          $scope.movieListID.original_title,
  image:          $scope.movieListID.poster_path,
  movie_id:       $scope.movieListID.id
}).then(init);

我添加参数去我movies_controller.rb

I've added the param to my movies_controller.rb

def movie_params
  params.require(:movie).permit(:title, :image, :release_date, :youtube_trailer, :imdb_rating, :movie_id)
end

和我在我的电影表中创建一个列

And I've created a column in my movies table

t.string   "youtube_trailer"

但是,当我检查我的movies.json它说 youtube_trailer:空

要回顾一下,我得到这样的输出,

To recap, I get this kind of output,

{"id":1893,"results":[
  {"id":"533ec65ac3a3685448000a24","iso_639_1":"en","key":"bD7bpG-zDJQ","name":"Trailer 1","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"533ec65ac3a3685448000a25","iso_639_1":"en","key":"UgDhFgSTPIw","name":"Trailer 2","site":"YouTube","size":720,"type":"Trailer"}
]}

我试图来存储键在我的数据库值,这样我就可以在以后使用它们。

And I'm trying to store both key values in my database so I can use them later.

推荐答案

您期望 youtube_trailer ,但创造这个字段作为 YO​​UTUBE_LINK 。请尝试匹配的变量名。

You are expecting youtube_trailer but create this field as youtube_link. Please try to match the variable names.

这篇关于保存多个值代入列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆