Forum: Ruby on Rails Agile Rails book: depot problem 2

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A07ec64186c6d821ca51abb2df646dda?d=identicon&s=25 Johan Van mol (johan-vm)
on 2006-02-05 12:01
My second problem is:

Listing users, and deleting them doesn't work.

If I try to list user, i just get this list:

User Delete
User Delete
User Delete
User Delete

I'm using this code in the login_controller:

  def list_users
  	@all_users = User.find(:all)
  end

  def delete_user
  	@id = params[:id]
  	if id && user = User.find(id)
  		begin
  			User.destroy
  			flash[:notice] = "User #{user.name} deleted"
  		rescue
  			flash[:notice] = "Can't delete that user"
  		end
  	end
  	redirect_to_index(:action => :list_users)
  end

and the list_users view looks like this:

<% for users in @all_users -%>
	<tr>
		<td><%= User.name %></td>
		<td><%= link_to 'Delete', { :action => 'delete_user', :id => id },
:confirm => 'Are you sure?' %></td>
	</tr>
	<% end -%>

trying to delete a user gives:

ActiveRecord::RecordNotFound in Login#delete_user
Couldn't find User with ID=30034968

By the way, I got this errormessage by clicking:

<a href="/login/delete_user/29113692" onclick="return confirm('Are you
sure?');">Delete</a>

The user ID's seem to be messed up, and I don't see what I did wrong...

Johan.
Ad91bce9c9a8c68b6942607e4721f74e?d=identicon&s=25 Beate Paland (Guest)
on 2006-02-05 14:13
(Received via mailing list)
Hi,

try

<% for user in @all_users -%>
       <tr>
               <td><%= user.name %></td>
               <td><%= link_to 'Delete', { :action => 'delete_user',
:id => user },
:confirm => 'Are you sure?' %></td>
       </tr>
       <% end -%>

HTH, Beate
A07ec64186c6d821ca51abb2df646dda?d=identicon&s=25 Johan Van mol (johan-vm)
on 2006-02-05 14:37
Thank you, listing users is working now.
But I still get an error message if I try to delete one:

--> http://localhost:3001/login/delete_user/1

>ActiveRecord::RecordNotFound in Login#delete_user
>Couldn't find User with ID=30085920

Apparantly, I should have used user.name instead of User.name...
Could you explain me why I shouldn't use a capital U in User.name?

Thank you,

Johan.
Ad91bce9c9a8c68b6942607e4721f74e?d=identicon&s=25 Beate Paland (Guest)
on 2006-02-05 15:10
(Received via mailing list)
2006/2/5, Johan Van Mol <johan.van.mol@gmail.com>:
> Thank you, listing users is working now.
> But I still get an error message if I try to delete one:
>
> --> http://localhost:3001/login/delete_user/1
>
> >ActiveRecord::RecordNotFound in Login#delete_user
> >Couldn't find User with ID=30085920

there is a typo in your controller. This does not work:
@id = params[:id]
       if id && user = User.find(id)

Try this:
 @id = params[:id]
    if @id && user = User.find(@id)

> Apparantly, I should have used user.name instead of User.name...
> Could you explain me why I shouldn't use a capital U in User.name?

You can't use "User", as this is not definded in the view:
<% for user in @all_users -%>
I myself visualize this like "For every single user in @users", so
"user" is my variable.

Beate
A07ec64186c6d821ca51abb2df646dda?d=identicon&s=25 Johan Van mol (johan-vm)
on 2006-02-05 15:39
Your suggested code didn't work.
I don't get an error message anymore,
but I get a flash notice: actionlist_users.
And the user doesn't get deleted.

However, I managed to fix my problem:

  def delete_user
    id = params[:id]
    if id && user = User.find(id)
  		begin
  			user.destroy
  			flash[:notice] = "User #{user.name} deleted"
  		rescue
  			flash[:notice] = "Can't delete that user"
  		end
  	end
  	redirect_to(:action => :list_users)
  end

Again, I typed User.destroy instead of user.destroy.
But you can use User.find instead of user.find.
I still don't really get it: when do you have to use capital characters
/ when not?

Beate, thank you for helping me with my 2 problems!

Johan
Ad91bce9c9a8c68b6942607e4721f74e?d=identicon&s=25 Beate Paland (Guest)
on 2006-02-05 16:32
(Received via mailing list)
2006/2/5, Johan Van Mol <johan.van.mol@gmail.com>:

> Again, I typed User.destroy instead of user.destroy.
> But you can use User.find instead of user.find.
> I still don't really get it: when do you have to use capital characters
> / when not?

User.find(...) means the model, i.e. find all or the first (depending
on your find) User from the table. user.destroy means the user set in
"user = User.find(id)", a special user.

But i can understand you, this was a little bit confusing to me in the
beginning, too.

Beate
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-05 17:14
(Received via mailing list)
Lowercase "user" is an instance variable... you can use it if you've
created or retrieved
a User into it (though you could also retrieve any other object into it
or you could call
the variable "rumplestiltskin"... but that would be more confusing). The
uppercase "User"
is the actual class.

So, when you do User.destroy you are calling a "class method", meaning
the method is not
tied to an instance (a "static" method in Java). This shouldn't work
because "destroy" is
not a class method.

When you do user.destroy you are calling an "instance method", meaning
the method is tied
to a specific instance of User... and it does the work declared in that
method *to* the
instance you call it on. This should delete the row in the db that
corresponds to the User
object held in variable "user".

The destroy method makes sense as an instance method, because you're
destroying a specific
instance. The find method, on the other hand, makes sense as a class
method because you
don't have an instance to call find on until you call find!

Hope that helps.

b
A07ec64186c6d821ca51abb2df646dda?d=identicon&s=25 Johan Van mol (johan-vm)
on 2006-02-05 19:00
It helps a lot!
Everything is clear now.

So this code:

id = params[:id]
  if id && user = User.find(id)
    begin
      user.destroy
      flash[:notice] = "User #{user.name} deleted"

actually does this:

It creates an instance variable id, based on the specified parameters.
If this id was created, a user instance variable is created, based on a
User in the User class, with the specified id.
Then this user instance variable (thus the User with the id you
specified) is destroyed, and a flash message appears.

And by typing in User.destroy - if this method existed - ,you would do
something like deleting all users?

Thank you all for your help!  I love the RoR community :-)

Johan.
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-05 20:58
(Received via mailing list)
Johan Van Mol wrote:
>
> actually does this:
>
> It creates an instance variable id, based on the specified parameters.
> If this id was created, a user instance variable is created, based on a
> User in the User class, with the specified id.
> Then this user instance variable (thus the User with the id you
> specified) is destroyed, and a flash message appears.
>
> And by typing in User.destroy - if this method existed - ,you would do
> something like deleting all users?

Well, if User.destroy existed, it might do something like that... but it
doesn't. You've
got it though... use class methods (defined by doing "def
self.methodname...") to do stuff
that doesn't make sense to have attached to a single instance and use
instance methods to
do stuff that does.

b
This topic is locked and can not be replied to.