2011-04-11 207 views
0

我得到这个错误:为什么我得到这个错误?

ActiveRecord::RecordNotFound (Couldn't find Video without an ID): 
    app/controllers/videos_controller.rb:52:in `topic_update' 

它指的是这个动作在我的视频控制器:

def topic_update 
    @video = Video.find(params[:id]) 

    respond_to do |format| 
    if @video.update_attributes(params[:video]) 
     format.html { redirect_to(@video) } 
     format.js 
    else 
     format.html { render :action => "edit" } 
    end 
end 
end 

错误是这种形式抛出后发送一个PUT请求:

<%= form_for @video, :url => {:action => "topic_update"}, :remote => true do |f| %> 
    <div class="field"> 
    <%= f.text_field :topic_names, :class => "topic_field" %> 
    </div> 
    <%= f.submit "Add Topic", :id => 'topic_submit' %> 
<% end %> 

这是根据我的日志发生的情况:

Started PUT "/topic/update.js" for 127.0.0.1 at Mon Apr 11 00:12:19 -0700 2011 
    Processing by VideosController#topic_update as JS 
    Parameters: {"video"=>{"topic_names"=>"eedefva"}} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1 

我有这个在我的routes.rb文件:

resources :videos 
match '/topic/update' => "videos#topic_update" 
+0

我该如何解决这个问题?如果我将表单更改为':url => {:action =>“update”}',那么它会突然生效......但是我想要转到topic_update方法 – 2011-04-11 04:26:15

+0

您没有将'id'参数传入请求。检查你的route.config文件。检查网址以查看您是否将id作为参数传递。 – 2011-04-11 04:27:14

回答

2

这是因为你的“topic_update”方法当作哪里,只要你想它作为一个POST方法“GET”方法,

试试这个在您的routes.rb

resources :videos do 
    member do 
    put 'topic_update' 
    end 
end 

我没有测试过这一点,但:d

在这里读到更多的信息(HTT电话号码://guides.rubyonrails.org/routing.html)

HTH

欢呼

sameera

0

基本上,你要更新一个不存在的(不是之前保存的)对象。 如果@video指的是之前保存的现有记录,则此方法form_for @video将添加id参数。

只有当@video对应于存储的记录时,请确保您正在调用更新过程(显示“编辑”窗体)。

相关问题