0

So I'm building an app that let's a user paste the url of a video, and then using the embedly jquery API it outputs the thumbnail. Then, when clicking the thumbnail, the embedded video appears.

However, I get this error in my controller:

NoMethodError in VideosController#create

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.save

Here's my video controller:

def new
  @video = Video.new
end

def create
  @video = Video.new(params[:video])

  respond_to do |format|
    if @article.save
      format.html #{ redirect_to(@video, :notice => 'Article was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

Here's my application.js file:

$(document).ready(function() {

$("li a").embedly({}, function(oembed, dict){
  if ( oembed == null)
    return;
  var output = "<a class='embedly' href='#'><img src='"+oembed.thumbnail_url+"' /></a><span>"+oembed.title+"</span>";
  output += oembed['code'];
    $(dict["node"]).parent().html( output );
});
   $('a.embedly').live("click", function(e){
    e.preventDefault();
    $(this).parents('li').find('.embed').toggle();
  });
});

And here's my new.html.erb view:

 <%= form_for(@video) do |f| %>
 <% if @video.errors.any? %>
   <div id="errorExplanation">
     <h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2>

  <ul>
  <% @video.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>
<div class="field">
  <%= f.label :video_url %><br />
  <%= f.text_field :video_url %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

 <div id="video_div">
<li><a href="<%= @video.video_url %>">Video</a></li>
 </div>

What am I doing wrong here? What are the next steps I need to take to make this work?

P.S. I include all the necessary files.

2 Answers 2

2
def create
  @video = Video.new(params[:video])

  respond_to do |format|
    if @article.save

video or article?

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

Comments

1

In the video controller you're trying to save @article. You want to save @video.

7 Comments

That means you don't have a create.html.erb view. At this point we're really getting into a different problem than your original question.
That's because you probably don't have a view for the create action. In practice, most people will do a redirect to an action displaying the record (e.g. show, index or whatever you prefer).
yeah, I realized that so I just redirect back to the new view with format.html { render :action => "new" } in the controller. However, now the thumbnail of the video doesn't appear. There's just a link that says "video" that takes you to the youtube video.
Right; you will want to return a different response to an ajax request than if it was a "normal" http request. You could return an html fragment or some json to work with back on the client side.
yeah, so i do something like format.js or format.json in the controller? and what's the html fragment I should be returning? Sorry for the noob questions lol
|

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.