admin管理员组

文章数量:1289543

I am practicing this RoR tutorial project of Michael Hartl:

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

I am here now: :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.

I am practicing this RoR tutorial project of Michael Hartl: http://ruby.railstutorial/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/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.

Share Improve this question edited Jun 2, 2013 at 18:42 Andrew Barber 40.2k20 gold badges97 silver badges124 bronze badges asked Oct 16, 2011 at 20:08 MiGMiG 431 gold badge2 silver badges7 bronze badges 1
  • Maybe @mhartl stackoverflow./users/57750/mhartl can answer to this? – MiG Commented Oct 16, 2011 at 20:13
Add a ment  | 

5 Answers 5

Reset to default 7

try using

  <%= javascript_include_tag "application" %>

instead of :defaults

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

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

In my current Rails 3.1 projects I use

<%= link_to 'Destroy', rest_datatype, :confirm => 'Are you sure?', :method => :delete %>

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 piled or loaded. Therefore, method method:: delete didn't work. Try to delete this.

本文标签: javascriptWhy Delete link doesn39t work in this Ruby on Rails projectStack Overflow