因此,我正在构建一个应用程序,让用户粘贴视频的网址,然后使用嵌入式jQuery API输出缩略图。然后,当点击缩略图时,出现嵌入的视频。这个Ruby on Rails和jQuery代码有什么问题?
不过,我得到这个错误在我的控制器:
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
这里是我的视频控制器:
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
这是我的application.js文件:
$(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();
});
});
这是我的new.html.erb
查看:
<%= 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>
我在做什么错在这里?我需要采取什么措施来完成这项工作?
P.S.我包含了所有必需的文件。
这意味着您没有'create.html.erb'视图。在这一点上,我们真的陷入了与原来的问题不同的问题。 – 2011-03-12 23:48:31
这是因为您可能没有创建操作的视图。在实践中,大多数人会重定向到显示记录的动作(例如显示,索引或任何你喜欢的)。 – Brian 2011-03-12 23:50:24
是的,我意识到,所以我只是在控制器中用'format.html {render:action =>“new”}'重新引导回'new'视图。但是,现在视频的缩略图没有出现。只有一个说“视频”的链接会将你带到YouTube视频。 – 2011-03-12 23:55:53