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 开发者_如何学Cdoing wrong here? What are the next steps I need to take to make this work?
P.S. 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
.
精彩评论