简体   繁体   中英

What's wrong with this Ruby on Rails & jQuery code?

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?

PS I include all the necessary files.

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

  respond_to do |format|
    if @article.save

video or article?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM