为什么我的RoR应用程序出现此错误?

问题描述:

我得到这个错误:为什么我的RoR应用程序出现此错误?

NoMethodError in Videos#new 

Showing /rubyprograms/dreamstill/app/views/videos/new.html.erb where line #1 raised: 

undefined method `videos_path' for #<#<Class:0x10398f8d8>:0x10398dbc8> 

我有一个视频模式,用newcreate方法的视频控制器。我的routes.db文件有root :to => "videos#new"。我有一个观点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 %> 

我的控制器有这样的:

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 

这是所有在我的路线文件:

Dreamstill::Application.routes.draw do 
    root :to => "videos#new" 
end 
+4

显示您的航线 – fl00r 2011-03-12 21:28:23

+0

确定发布,这不是很多...这可能是为什么我得到这个错误? – 2011-03-12 21:30:35

你的路线应该是

Dreamstill::Application.routes.draw do 
    root :to => "videos#new" 
    resources :videos 
end 
+0

嗯,我其实试过这个,但它没有工作......但现在它确实。谢谢! – 2011-03-12 21:35:23

+0

关键是“资源:视频”这一行。这就是允许您使用'videos_path','new_video_path'等等。请参阅[here](http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions)以获取更多信息。 – Zabba 2011-03-12 21:36:05