Delete action is not working and redirects to edit action

Hello, i have a strange problem here. I have made a teacher controller using scaffold. All actions work fine but the delete does not. When i click the delete link, it redirects me to the edit of this teacher. I have restarted the server and i have tried in firefox (i use chrome) but problem still remains. Here is the code from my view.

   <%= link_to(image_tag("/images/ delete_icon.jpg",:size=>"20x20",:border=>"0"), teacher, :confirm => 'Are you sure?', :method => :delete)%>

also tried but i get the same action

   <%= link_to 'Delete', teacher, :confirm=>"Are you sure?", :method=>:delete %>

The teacher_controller action is:   # DELETE /teachers/1   # DELETE /teachers/1.xml   def destroy     @teacher = Teacher.find(params[:id])     @teacher.destroy     respond_to do |format|       format.html { redirect_to(teachers_url) }       format.xml { head :ok }     end   end

In the routes i only use resources :teachers. Any sollution with that?? Thank u i advance.

Hello, i have a strange problem here. I have made a teacher controller using scaffold. All actions work fine but the delete does not. When i click the delete link, it redirects me to the edit of this teacher. I have restarted the server and i have tried in firefox (i use chrome) but problem still remains. Here is the code from my view.

Have a look in the log file (log/development.log) to see what is logged when you click the link

Colin

Sounds like you don't have any javascript files in your Rails app. Remember that the destroy method uses javascript to work correctly. If javascript is missing, then the browser sends a POST request to your Rails server. If you send a post request to an existing record, you get the edit action.

Make sure you have your javascript files included in confic/application.rb

Oops. Correction on the above answer. It should be GET instead of POST. If there is no javascript, the delete link sends a get request to your server. A get request on an existing record gives you the edit action.

Hi Colin, the log file shows the above:

Started GET "/teachers" for 127.0.0.1 at Thu Nov 04 22:16:06 +0200 2010   Processing by TeachersController#index as HTML   e[1me[36mSQL (40.0ms)e[0m e[1m SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' e[0m   e[1me[35mTeacher Load (0.0ms)e[0m SELECT "teachers".* FROM "teachers" Rendered shared/_header.html.erb (1.0ms) Rendered teachers/index.html.erb within layouts/teachers (31.0ms) Completed 200 OK in 467ms (Views: 59.0ms | ActiveRecord: 40.0ms)

Started GET "/teachers/11" for 127.0.0.1 at Thu Nov 04 22:16:08 +0200 2010   Processing by TeachersController#show as HTML   Parameters: {"id"=>"11"}   e[1me[36mTeacher Load (0.0ms)e[0m e[1mSELECT "teachers".* FROM "teachers" WHERE ("teachers"."id" = 11) LIMIT 1e[0m Rendered shared/_header.html.erb (2.0ms) Rendered teachers/show.html.erb within layouts/teachers (23.0ms) Completed 200 OK in 147ms (Views: 61.0ms | ActiveRecord: 0.0ms)

Started GET "/teachers" for 127.0.0.1 at Thu Nov 04 22:16:09 +0200 2010   Processing by TeachersController#index as HTML   e[1me[35mTeacher Load (1.0ms)e[0m SELECT "teachers".* FROM "teachers" Rendered shared/_header.html.erb (2.0ms) Rendered teachers/index.html.erb within layouts/teachers (29.0ms) Completed 200 OK in 55ms (Views: 43.0ms | ActiveRecord: 1.0ms)

Do you see anything strange?

Hi Arailsdemo, do i have to write something in the application.rb file?? I havent edited this file. The only thing i have inside is:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(:default, Rails.env) if defined?(Bundler)

module Ptixiakes   class Application < Rails::Application     config.encoding = "utf-8"     config.filter_parameters += [:password]   end end

Thank u in advance both Kostas

Hi Colin, the log file shows the above:

Started GET "/teachers" for 127.0.0.1 at Thu Nov 04 22:16:06 +0200 2010 Processing by TeachersController#index as HTML [1m [36mSQL (40.0ms) [0m [1m SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' [0m [1m [35mTeacher Load (0.0ms) [0m SELECT "teachers".* FROM "teachers" Rendered shared/_header.html.erb (1.0ms) Rendered teachers/index.html.erb within layouts/teachers (31.0ms) Completed 200 OK in 467ms (Views: 59.0ms | ActiveRecord: 40.0ms)

Started GET "/teachers/11" for 127.0.0.1 at Thu Nov 04 22:16:08 +0200 2010 Processing by TeachersController#show as HTML Parameters: {"id"=>"11"} [1m [36mTeacher Load (0.0ms) [0m [1mSELECT "teachers".* FROM "teachers" WHERE ("teachers"."id" = 11) LIMIT 1 [0m Rendered shared/_header.html.erb (2.0ms) Rendered teachers/show.html.erb within layouts/teachers (23.0ms) Completed 200 OK in 147ms (Views: 61.0ms | ActiveRecord: 0.0ms)

Started GET "/teachers" for 127.0.0.1 at Thu Nov 04 22:16:09 +0200 2010 Processing by TeachersController#index as HTML [1m [35mTeacher Load (1.0ms) [0m SELECT "teachers".* FROM "teachers" Rendered shared/_header.html.erb (2.0ms) Rendered teachers/index.html.erb within layouts/teachers (29.0ms) Completed 200 OK in 55ms (Views: 43.0ms | ActiveRecord: 1.0ms)

Do you see anything strange?

Well there is no POST for the delete apparently. At what point in the above did you click the delete link? If you are not sure then repeat it and note the times that you do things. Leave a bit between actions if necessary.

Have you checked the html for your link to make sure it looks ok (View

Page Source or similar in your browser)?

Colin

I had this same problem when starting a new Rails 3 project with jQuery. (I generated this project using a template from the Rails Wizard website.)

If you are using jQuery, make sure you have the correct rails.js file (the jquery-rails gem will add rake tasks to help manage this).

Also make sure that the jquery.min file is being loaded with the rails.js file My layout was using "javascript_include_tag :defaults", but only my rails.js file was getting loading.

I fixed this by updating the config/application.rb file and modifying the "config.action_view.javascript_expansions[:defaults]" setting to add jquery.min.

As a correction, the jquery-rails gem adds a generator (rails generate jquery:install), not rake tasks.

Can you confirm whether you see the Are You Sure popup? If not then, as others have suggested, there is an issue with the javascript. Check that you have <%= javascript_include_tag :defaults %> in the layout (application.html.erb unless you have provided your own layout).

Colin

Hi guys, the problem was javascript!!! I've added this lines in my layout and it worked:

<%= javascript_include_tag :defaults %>

Thank you for your time!!!! Greetings Kostas