Ruby on Rails& amp;有什么问题? jQuery代码?

时间:2011-03-12 23:40:07

标签: jquery ruby-on-rails ruby ajax

所以我正在构建一个应用程序,让用户粘贴视频的网址,然后使用嵌入式jquery API输出缩略图。然后,单击缩略图时,将显示嵌入的视频。

但是,我在控制器中收到此错误:

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。我包含了所有必要的文件。

2 个答案:

答案 0 :(得分:2)

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

  respond_to do |format|
    if @article.save

视频或文章?

答案 1 :(得分:1)

在视频控制器中,您尝试保存@article。您要保存@video