开发者

Why do I get this error?

开发者 https://www.devze.com 2023-02-23 20:52 出处:网络
I\'m getting this error: ActiveRecord::RecordNotFound (Couldn\'t find Video without an ID): app/controllers/videos_controller.rb:52:in `topic_update\'

I'm getting this error:

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

It's referring to this action in my videos controller:

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 =&开发者_StackOverflowgt; "edit" }
    end
 end
end

The error is thrown after this form sends a PUT request:

<%= 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 %>

This is what happens according to my logs:

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

I have this in my routes.rb file:

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


This is because your 'topic_update' method will treat as a 'GET' method where as you want it as a post method,

try this in your routes.rb

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

I haven't tested this but :D

read here for more info (http://guides.rubyonrails.org/routing.html)

HTH

cheers

sameera


Basically, you are trying to update a non-existent (not saved before) object. This method form_for @video would add id parameter if @video is referring to an existent record that was saved before.

Please make sure you're calling update procedure (showing "edit" form) only if @video corresponds to a stored record.

0

精彩评论

暂无评论...
验证码 换一张
取 消