2011-03-12 77 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 

視頻或文章?