1

I am working in an interface, that needs information from a video model I created. That means I will need to execute a MySQL query, to get information I need. My code is this, in my interface.js.erb file:

...
    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");
        }

    }
...

The above code returns me this:

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" %>  

should I use a normal *.js file? what are the main differences and how do I fix my problem?

EDIT:

  # 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
8
  • According to you error message, the @video variable is not set as low_fps in an undefined method. Did you check whether you actually defined @video in your controller? Commented Jun 21, 2012 at 10:50
  • which controller? my VideosController was generated automatically by Rails Commented Jun 21, 2012 at 10:52
  • @TestTest Whatever controller is handling the request when the interface.js.erb file is being rendered when it throws the error. Commented Jun 21, 2012 at 10:54
  • that controller :) as it should define the @video is not defined. It also might be possible that your controller was not able to find the video because it does not exist and the controller does not check this properly. If you still need more help, post (relevant parts of) the controller code (like the show method). Commented Jun 21, 2012 at 10:55
  • I dont get it. I am not using any controller to handle the interface.js.erb. I just included the file in the application.html.erb in the layout folder. I am kinda new to Rails 3 so any help will be useful. I have edited the question Commented Jun 21, 2012 at 10:59

1 Answer 1

2

In you view (videos/show.html.erb?) try doing following

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

and then change your JS function:

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");
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I will try that later, but the thing is that I am trying to understand now how you can render, *.js using a controller like @veger said. my *.js file contains only a function, so I dont understand what there is to render. I just wanna pass a rails variable to the *.js function
To use such ruby variables you have to respond with such action - you can use <%= @video.id %> in ie. videos/show.js.erb - to (for example) overwrite some values, like number of votes. show.js.erb will be rendered after you make ajax request: $.getScript('/videos/1')
I already have a show.html.erb file, will I have to add also the show.js.erb?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.