2011-03-12 81 views
0

因此,我正在构建一个应用程序,让用户粘贴视频的网址,然后使用嵌入式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.我包含了所有必需的文件。

回答

1

在视频控制器中,您试图保存@article。你想保存@video

+0

这意味着您没有'create.html.erb'视图。在这一点上,我们真的陷入了与原来的问题不同的问题。 – 2011-03-12 23:48:31

+0

这是因为您可能没有创建操作的视图。在实践中,大多数人会重定向到显示记录的动作(例如显示,索引或任何你喜欢的)。 – Brian 2011-03-12 23:50:24

+0

是的,我意识到,所以我只是在控制器中用'format.html {render:action =>“new”}'重新引导回'new'视图。但是,现在视频的缩略图没有出现。只有一个说“视频”的链接会将你带到YouTube视频。 – 2011-03-12 23:55:53

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

    respond_to do |format| 
    if @article.save 

视频或文章?