1

我正在一个界面中工作,该界面需要来自我创建的视频模型的信息。这意味着我需要执行 MySQL 查询,以获取我需要的信息。我的代码是这样的,在我的interface.js.erb文件中:

...
    function play_vid()
    {
        low_fps = '<%= @video.low_fps %>' ;
        alert(low_fps); //checking
        if(low_vid.paused){
            low_vid.play();
            play_icon.classList.remove("icon-play-circle");
            play_icon.classList.add("icon-pause");
        }else{
            low_vid.pause();
            play_icon.classList.remove("icon-pause");
            play_icon.classList.add("icon-play-circle");
        }

    }
...

上面的代码返回给我这个:

NoMethodError in Videos#show

Showing /home/user/My Projects/Ruby Projects/Video_APP/app/views/layouts/application.html.erb where line #8 raised:

undefined method `low_fps' for nil:NilClass
  (in /home/user/My Projects/Ruby Projects/Video_APP/app/assets/javascripts/jQuery/interface.js.erb)

Extracted source (around line #8):

5:   <%= stylesheet_link_tag    "application", :media => "all" %>
6:   <%= javascript_include_tag "jQuery/jquery-1.7.2.min.js" %>
7:   <%= javascript_include_tag "jQuery/jquery-ui-1.8.21.custom.min.js" %> 
8:   <%= javascript_include_tag "application.js" %>
9:   <%= javascript_include_tag "bootstrap-dropdown.js" %>
10:   <%= javascript_include_tag "jQuery/interface.js.erb" %>  
11:   <%= javascript_include_tag "jQuery/events_controls.js" %>  

我应该使用普通的 *.js 文件吗?主要区别是什么?如何解决我的问题?

编辑:

  # GET /videos/1
  # GET /videos/1.json
  def show
    @video = Video.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @video }
    end
  end
4

1 回答 1

2

在您看来 (videos/show.html.erb?) 尝试执行以下操作

<div id="video" data-lowfps="<%= @video.low_fps %>"></div>

然后更改您的 JS 函数:

function play_vid()
{
    low_fps = $('#video').data('lowfps');
    alert(low_fps); //checking
    if(low_vid.paused){
        low_vid.play();
        play_icon.classList.remove("icon-play-circle");
        play_icon.classList.add("icon-pause");
    }else{
        low_vid.pause();
        play_icon.classList.remove("icon-pause");
        play_icon.classList.add("icon-play-circle");
    }

}
于 2012-06-21T11:20:29.820 回答