【发布时间】:2015-03-31 12:14:05
【问题描述】:
大家好,我通过 WatchList 在用户电影之间进行了非常简单的多对多设置。这是一个在 iTunes api 中搜索电影的应用程序,在这里我添加了用户将搜索到的电影保存到个人列表的功能。我正在使用 Rails 4.1.1 和 sqlite3。
class User < ActiveRecord::Base
has_secure_password
validates :email, presence: true, uniqueness: true
validates :name, presence: true
validates :password, length: { minimum: 6 }, allow_nil: true
has_many :watch_lists
has_many :movies, through: :watch_lists
def add_movie(movie)
self.movies << movie
end
class Movie < ActiveRecord::Base
has_many :watch_lists
has_many :users, through: :watch_lists
end
class WatchList < ActiveRecord::Base
belongs_to :user
belongs_to :movie
end
我的问题是当我尝试添加使用 iTunes id 作为 DB :id 的电影时。在第 7 行的 saves_controller 中,它为 nil:NilClass 抛出未定义的方法“名称”。我想不通,没有什么是零?电影保存到数据库很好,有正确的信息。这是我尝试创建连接表的时候。什么是rails试图调用'name'???
第 7 行引发错误 current_user.movies undefined method 'name for nil:NilClass
class SavesController < ApplicationController
def create
temp_movie = aquire_movie
@movie = Movie.new(temp_movie.local_db_movie_info_hash)
if @movie.save
current_user.movies << @movie #### => LINE 7 ERROR undefined methond 'name for nil:NilClass
redirect_to @movie
end
end
private
def aquire_movie
movie_data = itunes_results[:results][0]
TemporaryMovie.new(movie_data)
end
def itunes_results
data_retriever.get_data(params[:movie_id], search_by_id: true)
end
结束
这里也是架构。
ActiveRecord::Schema.define(version: 20150330012300) do
create_table "movies", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.string "auth_token"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["auth_token"], name: "index_users_on_auth_token"
add_index "users", ["email"], name: "index_users_on_email"
create_table "watch_lists", force: true do |t|
t.integer "movie_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
更新
我刚让一个朋友克隆了这个 repo,他没有任何问题,所以这是某种 gem /database 问题??
【问题讨论】:
-
似乎是您的用户的问题,听起来绝对是数据问题。尝试从新开始这样做,看看会发生什么。删除整个数据库并从隐身浏览器登录。情况似乎是这样,因为它正在抱怨我只能将其视为您的用户的错误。
标签: ruby-on-rails ruby sqlite