2

I am practicing this RoR tutorial project of Michael Hartl: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

I am using Ruby 1.9.2 and Rails 3.1 on Ubuntu 11.04 with Apache and MySQL.

I am here now: http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec:destroying_users (where I have s list of users and a Delete link for each of them, me being the admin).

This is the delete link which supposed to work:

<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
                            :title => "Delete #{user.name}" %>

But when I mouseover I see this: "localhost/users/2" (for example) And when I click on the Delete link it directs me to the user's profile. I don't get any confirmation window and no action is done (delete). It's like a link to their profile and nothing else. I am using Chrome but in Firefox is the same.

This is what I have:

1)the gem "jquery-rails" is installed

2)applications.js (from app/assets/javascripts) has these lines:

//= require jquery
//= require jquery_ujs
//= require_tree .

3)application.html.erb (from app/views/layouts) has these lines:

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tags %>

In view source I see:

  <script src="/assets/defaults.js" type="text/javascript"></script>
  <meta content="authenticity_token" name="csrf-param" />
  <meta content="njPO91rB7p3EtTblD4jf4rkCVt+M76SKUt0rQhHc+qY=" name="csrf-token" />

4)in users_controllers.rb (from app/controllers) I have:

 before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
 before_filter :correct_user, :only => [:edit, :update]
 before_filter :admin_user,   :only => :destroy

 def destroy
  User.find(params[:id]).destroy
  flash[:success] = "User destroyed."
  redirect_to users_path
 end

 private
  def admin_user
    redirect_to(root_path) unless current_user.admin?
  end

5)in routes.rb (from config) I have:

resources :users

What is wrong with that Delete link? I've searched StackOverflow for answers but apparently I couldn't find one to suit my situation.

On the same RoR project the follow/unfollow button using Ajax works just fine (can't post a second link here for now because I just registered to the SO).

So, why the link doesn't work? Is it a Javascript problem or some other problem which I am not seeing?

Thanks.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
MiG
  • 43
  • 2
  • 7

5 Answers5

7

try using

  <%= javascript_include_tag "application" %>

instead of :defaults

daniel
  • 901
  • 1
  • 10
  • 21
  • 2
    This would be right. :defaults is a hangover from < 3.1 days. – Ryan Bigg Oct 16 '11 at 22:24
  • 3
    This is why Chapter 1 has the line (as of this writing) "gem install --version 3.0.11". Using any other version of Rails will lead to incompatibilities. (Note: The Rails 3.1 edition is in preparation.) – mhartl Nov 25 '11 at 19:07
5

try switching it to button_to "delete" instead of link_to "delete" That worked for me when nothing else would

mattc
  • 51
  • 1
  • 1
0

I've faced this recently. Try to look in the

config/application.rb:

Let's see if there is a string

Rails.logger = Logger.new(STDOUT)

Because of this, js libraries are not compiled or loaded. Therefore, method method:: delete didn't work. Try to delete this.

Ruslan Valeev
  • 1,529
  • 13
  • 24
0

Make sure you have resources :users in your routes.rb file. That should give you the basic CRUD routing.

Joe
  • 2,987
  • 15
  • 24
  • Yes, I have these in routes.rb: resources :users resources :sessions, :only => [:new, :create, :destroy] – MiG Oct 16 '11 at 21:03
0

In my current Rails 3.1 projects I use

<%= link_to 'Destroy', rest_datatype, :confirm => 'Are you sure?', :method => :delete %>
Ben
  • 13,297
  • 4
  • 47
  • 68
  • I get this error: undefined local variable or method `rest_datatype' – MiG Oct 16 '11 at 21:05
  • that's Benjamin's model. Yours is user – Michael Durrant Oct 16 '11 at 21:10
  • Well, this "<%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %>" doesn't work either. I get the same situation (going to the profile and nothing happens in between). – MiG Oct 16 '11 at 21:14