Antarr Byrd
Antarr Byrd

Reputation: 26061

Unable to delete record, user

I'm trying to delete using the delete action. But whenever I go click the link i just goes to the users profile.

User Controller

class UsersController < ApplicationController

   filter_resource_access

  # GET /users
  # GET /users.xml
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  # GET /users/1
  # GET /users/1.xml
  def show
    #@user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
    #@user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

  # GET /users/1/edit
  def edit
    #@user = User.find(params[:id])
  end

  def create
    #@user = User.new(params[:user])
    @user.channels << Channel.find(1)

    respond_to do |format|
      if @user.save
        format.html { redirect_to(:channels, :notice => 'Registration successfully.') }
        format.xml { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  def profile
    @user = User.find(params[:id])
  end



  # PUT /users/1
  # PUT /users/1.xml
  def update
  #@user = current_user

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end

  def delete
    @user = User.find(params[:user_id])
    @user.destroy
    redirect_to :users
  end

end

Index User View

<h1>Listing users</h1>
<p id="notice"><%= notice %></p>
<table>
  <tr>
    <th>Username</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Telephone</th>
    <th>Email</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @users.each do |user| %>
  <tr>
    <td><%= user.login %></td>
    <td><%= user.first_name %></td>
    <td><%= user.last_name %></td>
    <td><%= telephone_field_tag :phone, user.telephone ,:disabled => true %></td>
    <td><%= email_field_tag :email, user.email, :disabled => true %></td>
    <td><%= link_to 'View Profile', user %></td>
    <td><%= link_to 'Edit Profile', edit_user_path(user) if permitted_to? :update ,user %></td>
    <%if permitted_to? :delete ,user   %>
    <td><%= link_to 'Close Account', user, :method => :delete, :confirm => "Are you sure?" %></td>
    <% end %>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New User', new_user_path %>

Upvotes: 2

Views: 1330

Answers (3)

jdl
jdl

Reputation: 17790

If you are trying to do this with Rails 3, you should probably read this SO question.

If you're trying to do this with Rails 2, some logs would help us help you.

Upvotes: 1

nowk
nowk

Reputation: 33161

If you are using Rails 3: the old link_to delete stuff doesn't work like it used to.

It's all UJS now. I'd make sure you are including all the javascript goodies, either the default prototype based stuff or use the jquery ones, if that is more your style.

Also, make sure you have the csrf_meta_tag in your layouts. more...

Upvotes: 1

Jaime Bellmyer
Jaime Bellmyer

Reputation: 23307

Chances are good that you're using Rails 3, right? This is a common problem. In rails 2.x, the link_to helper used to create messy inline javascript to create form that was submitted with the appropriate delete call (kind of, but good enough for this explanation).

In Rails 3 however, the javascript that actually does this has been moved outside of the views themselves, into javascript files. This is known as "unobtrusive" javascript, where the HTML source code is strictly for document structure, and document behavior lives elsewhere.

The bottom line is that your layout needs to be including these files in the <head> section, like so:

<%= javascript_include_tag :all %>

That should fix your issue.

Upvotes: 5

Related Questions