Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 索引页没有';t识别模型(rails 4.0)中已定义的方法_Ruby On Rails_Iframe_Video_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 索引页没有';t识别模型(rails 4.0)中已定义的方法

Ruby on rails 索引页没有';t识别模型(rails 4.0)中已定义的方法,ruby-on-rails,iframe,video,ruby-on-rails-4,Ruby On Rails,Iframe,Video,Ruby On Rails 4,这件事我已经坚持了好几天了。在该模型中,我定义了“代码”,用于格式化要添加到iframe中的youtube链接。当我转到localhost:3000/视频时,它会告诉我 “用于#的未定义方法'code'” 视频索引 现在,如果我跳转到show页面(localhost:3000/videos/#),它可以正常工作。这是表演 <div class="row"> <div class="span8"> <ol class="video"&

这件事我已经坚持了好几天了。在该模型中,我定义了“代码”,用于格式化要添加到iframe中的youtube链接。当我转到localhost:3000/视频时,它会告诉我

“用于#的未定义方法'code'”

视频索引
现在,如果我跳转到show页面(localhost:3000/videos/#),它可以正常工作。这是表演

<div class="row">
    <div class="span8">
            <ol class="video">
                <iframe width="560" height="349" src="<%= "http://www.youtube.com/embed/"+@video.code %>"></iframe>
            </ol>
    </div>
</div>

我也看不出控制器中的索引有问题@视频=视频。一切看起来都很简单

class VideosController < ApplicationController

    def index
        @videos = Video.all
    end

    def show
        @video = Video.find(params[:id])
    end
class videoscocontroller
这就是模型。video.rb,我在其中定义了代码

class Video < ActiveRecord::Base
    has_many :comments
    belongs_to :user
    default_scope -> { order('created_at DESC') }
    validates :video, presence: true, length: { maximum: 420 }

    def code
      self.video.try(:split, '/').try(:last) || ''
    end
end
class-Video{order('created_at DESC')}
验证:视频,状态:真,长度:{最大值:420}
def代码
self.video.try(:split,“/”)。try(:last)| |“
结束
结束
这是用户,这是视频所属的用户

class User < ActiveRecord::Base
  has_many :videos
class用户

感谢对响应者的帮助,请务必给我一个代表点,这样我就可以在15岁时为您回电话;)

@videos
不应该有这样的方法
code
,它应该是一个实例方法

您需要在循环中使用
video
而不是
@videos

此外,视图代码看起来很phpis。它需要改进

重构 而不是

<iframe width="560" height="349" src="<%= "http://www.youtube.com/embed/"+@videos.code %>">
</iframe>

抱歉,这是整个错误“未定义的#方法'code'”
<iframe width="560" height="349" src="<%= "http://www.youtube.com/embed/"+@videos.code %>">
</iframe>
# View
video_iframe video.code

# Helper
def video_iframe(src)
  content_tag(:iframe, width: 500, height: 349, src: src)
end

# Model
def code
  if process_video.present?
    "http://www.youtube.com/embed/#{process_video}"
  end
end

private
def process_video
  video.try(:split, '/').try(:last) || ''
end