未定义的局部变量或方法`video_url'

时间:2015-08-28 21:55:00

标签: ruby-on-rails

我正在关注如何实现braintree的教程但是我遇到了一个错误,同时尝试rake db:seed或者在行动中#show:

rake aborted!
NameError: undefined local variable or method `video_url' for #    <Movie:0x007ff0c9fd4028>
/.../.../.../tutorial/billingleap2/moviestore/app/models/movie.rb:16:in `embed_video_url'
/.../.../.../tutorial/billingleap2/moviestore/db/seeds.rb:3:in `block in <top (required)>'
/.../.../.../tutorial/billingleap2/moviestore/db/seeds.rb:2:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

以下是我的种子数据:https://github.com/claucece/cartandbraintree/blob/purchase/db/seeds_data/movies.csv

还有我的电影模型:

class Movie < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases

before_save :embed_video_url

def poster
"http://ia.media-imdb.com/images/M/#{poster_url}"
 end

def imdb
"http://www.imdb.com/title/#{imdb_id}/"
end

def embed_video_url
self.video_url = "//www.youtube.com/embed/#{video_url.split('v=') [1].split('&list')[0]}"
end

def cart_action(current_user_id)
if $redis.sismember "cart#{current_user_id}", id
  "Remove from"
else
  "Add to"
end
end
end

我想我错过了一些非常简单的事情......但不知道是什么。

1 个答案:

答案 0 :(得分:1)

undefined local variable or method `video_url' for #    <Movie:0x007ff0c9fd4028>

这意味着,它找不到电影对象的video_url方法。确保您的movies表在数据库中有vider_url列。

所以,这是你的架构: https://github.com/claucece/cartandbraintree/blob/purchase/db/schema.rb#L14-L25

而且,video_url表格中没有movies。这就是你犯这个错误的原因。

要解决此问题,您只需创建迁移即可在video_url表格中添加movies列。

rails generate migration AddVideoUrlToMovies video_url:string
bundle exec rake db:migrate

然后,它将按预期工作。