在数据库中存储多个值

时间:2015-12-04 13:59:27

标签: ruby-on-rails angularjs

我的SQLite数据库中有两个表。在名为movies的表格和名为trailers的表格上。电影表有几列,

create_table "movies", force: :cascade do |t|
  t.string   "title"
  t.string   "release_date"
  t.string   "image"
  t.integer  "user_id"
  t.datetime "created_at",   null: false
  t.datetime "updated_at",   null: false
  t.string   "movie_id"
  t.string   "imdb_rating"
end

我想在电影中添加预告片。我通过在trailers表中添加一个名为movies的列,然后将多个结果存储在一列中来尝试这种方法。但这看起来不是正确的方法。

所以我创建了trailers表。

create_table "trailers", force: :cascade do |t|
  t.string   "movie_id"
  t.string   "link"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

我的想法是使用movie_id值保存每个预告片链接,这样我就可以合并我在Angular应用程序中使用的1个JSON文件中的两个表。

要检索我已创建服务的预告片数据,

service.trailer = function(youtube_link){
  return httpPromise(
    baseUrl + youtube_link + '/videos?api_key=a8f7039633f2065942***a28d7cadad4&callback=JSON_CALLBACK'
  )
};

从该服务返回的数据如下所示,

{"id":157336,"results":[
  {"id":"53db3c790e0a26189a000d09","iso_639_1":"en","key":"ePbKGoIGAXY","name":"Trailer 3","site":"YouTube","size":1080,"type":"Trailer"},
  {"id":"550df44b9251413554004d43","iso_639_1":"en","key":"KlyknsTJk0w","name":"Own it today","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"533ec6fcc3a3685448009ccc","iso_639_1":"en","key":"nyc6RJEEe0U","name":"Teaser","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"5376ab510e0a26141c0005a8","iso_639_1":"en","key":"zSWdZVtXT7E","name":"Trailer","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"545da247c3a3685362005187","iso_639_1":"en","key":"Lm8p5rlrSkY","name":"Trailer 2","site":"YouTube","size":1080,"type":"Trailer"}
]}

所以现在我正在尝试将数据保存到trailer表中。

var mappedData = dataYoutube.results.map(function(r) {
  var obj = {}
  obj["movie_id"] = dataYoutube.id;
  obj["link"] = r.key
  return obj;
});
console.log(mappedData);

createTrailer.create({
  movie_id: mappedData.movie_id,
  link:     mappedData.key
})

我的服务中的createTrailer功能

app.factory('createTrailer', ['$http', function($http){
  return{
    create: function(trailer){
      return $http.post('/trailers.json', trailer);
    }
  };
}])

在Rails后端我创建了路由,

resources :trailers, only: [:create, :destroy, :index, :show]

一个trailers_controller.rb

class TrailersController < ApplicationController
  def index
    respond_with Trailer.all
  end
  def create
  end
end

还有一个trailer_model.rb

class Trailer < ActiveRecord::Base
  belongs_to :movie
end

目前,当我执行保存操作时,我在rails控制台中收到错误,

Started POST "/trailers.json" for 127.0.0.1 at 2015-12-04 16:04:25 +0100
Processing by TrailersController#create as JSON
  Parameters: {"movie_id"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "link"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "trailer"=>{"movie_id"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "link"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}]}}
  User Load (10.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 15ms (ActiveRecord: 17.1ms)

ActionView::MissingTemplate (Missing template trailers/create, application/create with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/alucardu/sites/movieseat/app/views"
  * "/home/alucardu/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views"
):

1 个答案:

答案 0 :(得分:1)

根据您上面的数据,您可以制作一个对象数组,每个对象都有$username = "username" $password = "password" $startWithElevatedRights = "notepad" $credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force)) $ps = Start-Process -PassThru -FilePath powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process ', $startWithElevatedRights, ' -Wait -verb runas}' $ps.WaitForExit() movie_id列:

link

小提琴:http://jsfiddle.net/muat8brp/