Simple if - then question

hi, could somebody tell me the difference between:

if not params[:id]
flash[:notice] = blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

and:

if params[:id] == nil
flash[:notice] = blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

the first one works, the second one doesn’t, but i don’t know why…

regards,

remco

On 8/7/06, Remco Hh [email protected] wrote:

if params[:id] == nil


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I don’t have ruby here with me at the moment since I’m at work so I
can’t
check this :frowning:

But I think it could be that params[:id] is not defined. So saying not
params[:id] would be true. but since it’s not defined then params[:id]
cannot == nil

To check that this is the case include something like

put “Params is defined #{params[:id].defined?}”

in your code and have a look.

On Monday, August 07, 2006, at 9:48 AM, Remco Hh wrote:

 flash[:notice] = blabla'


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Both of these should work provided that params does not contain an :id
key.

This is probably a better syntax though…

unless params[:id]
flash[:notice] = ‘blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

or

if params[:id].nil?
flash[:notice] = ‘blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

_Kevin
www.sciwerks.com

Kevin O. wrote:

On Monday, August 07, 2006, at 9:48 AM, Remco Hh wrote:

 flash[:notice] = blabla'


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Both of these should work provided that params does not contain an :id
key.

This is probably a better syntax though…

unless params[:id]
flash[:notice] = ‘blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

or

if params[:id].nil?
flash[:notice] = ‘blabla’
redirect_to(:controller =>“calendar”, :action => ‘index’)
end

_Kevin
www.sciwerks.com

thanks both for your reply!
kevin, you are right, this looks better and more ruby-like :slight_smile: