0

i have some troubles with understanding of "params" function in RoR.

let's assume that i have user Model. the next code apperars in users controller and it's function is to process POST 'destroy' request. "current_user" function returns currently signed in user (an instance of User class I suggest). by comparing "current_user == params[:id]" 'destroy' function checks if user is trying to delete yourself

def destroy

    if current_user == params[:id]
        flash[:error] = "you cannot delete yourself!"
    else
        User.find(params[:id]).destroy
        flash[:success] = "user deleted"
    end

    redirect_to(users_path)
end

So the problem is that chunk code works well. and I don't really understand why. My background is 3-years experience of C++/C# programming in university, so I presumed that such kind of comparsion should cause some type casts. In that case I think it would be User obj ---> string obj (OR string --> User???!!!!). Although I have a lot of questions about how Rails manages to compare User class and string class, I could make myself comfortable with this.

But what if I want to optimize this task and explicitly compare just IDs: the one stored in params[:id] as a string(??) and the other in current_user["id"] hash.

first is of string type and second is of integer, am I wrong? because "current_user["id"] == params[:id].to_i" causes error, that implies that params[:id] returns instance of User class o_O

thanks!

2
  • 1
    Can you post the code for the current_user method? I would not expect current_user == params[:id] ever to return true, so I'm surprised that this code is working for you.
    – Brandan
    Feb 26, 2012 at 4:08
  • It's hard to tell what's going on as I don't know what the value of current_user or params is. You might want to check them with something like Rails.logger.debug("params: #{params.inspect}"). There might be some magic going on (like saying an object is equal to itself or its ID), but I would not expect a User compared to an integer to return true.
    – brymck
    Feb 26, 2012 at 4:39

3 Answers 3

0

first of all: get yourself a decent ruby book and read about it's dynamic type system and method execution. that should answer most of your questions that you have when you come from a language like c.

like the opperator overloading in c it's possible in ruby to implement custom behavior for things like ==. this is very easy in ruby, because == is just a method. that's how you could write comparisons for multiple types of classes, even though those are not symmetric anymore.

in your case, the code that you provided is just wrong. comparing current_user with params['id'] will always yield false.

you should write something like that:

user = User.find params[:id]
if current_user == user
  redirect_to users_path, :error => "you cannot delete yourself!"
else
  user.destroy
  redirect_to users_path, :notice => "user deleted"
end
3
  • thank you for piece of advice! I have tried that approach already - it works. but my interest was in little improvements. I wanted not to compare User objects, which as I think undergoes full attributes comparations, but compare native simple types Feb 27, 2012 at 12:19
  • there is no such thing as a native simple type in ruby. it's all objects and you need to know what == is doing anyways. nevertheless, current_user == user is good code style in comparison to what you are trying to do.
    – phoet
    Feb 27, 2012 at 13:06
  • thank you man, I get it! NEVERTHELESS, I would like to correct myself: term "native" was exessive here, I meant SIMPLE types. My english is still poor. Feb 28, 2012 at 6:57
0

Your current_user variable should contain the id in integer or string format. Or maybe your user model has a to_s method defined for user instances. In this case when trying to convert the object to a string (for the comparison with a string), this method will be called which will be returning the id in string format. You should print both the current_user variable as well as the params[:id] to be sure.

0

you should do this

if current_user.id.to_s == params[:id]

params[:id] is a string, and you should comparing it with the current_user's id , not current_user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.