if @params[:per_page].nil?
per_page = 20
else
per_page = @params[:per_page]
end
You got very bright answers from very good people, or was it the other way
round?
The answers were of type
var = val || default_val
As it seems that you are accessing a Hash object, you could do the following
which is semantically different and this is the main reason I anwser your
post.
per_page = @params.fetch( :per_page, 20 )
Now where is the difference:
if @params does not contain the key :per_page, 20 is returned, however if @params[:per_page] was set to nil or false, nil or false is returned
accordingly
It is up to you to know what you need
Hope that helps
Robert
Is there a better way to do this (put the parameter in the variable if
···
On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:
But the next problem is that if params[:per_page] equals 0 then per_page
equals 0 and I get the error message "must have at least one item per
page" from the Rails paginate option...
unknown wrote:
···
Hi --
On Mon, 28 Aug 2006, Nicolas Blanco wrote:
end
Is there a better way to do this (put the parameter in the variable if
the parameter is not null, otherwise put 20) ?
But the next problem is that if params[:per_page] equals 0 then per_page
equals 0 and I get the error message "must have at least one item per
page" from the Rails paginate option...
But the next problem is that if params[:per_page] equals 0 then per_page
equals 0 and I get the error message "must have at least one item per
page" from the Rails paginate option...
On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:
unknown wrote:
> Hi --
>
> On Mon, 28 Aug 2006, Nicolas Blanco wrote:
>
>> end
>>
>> Is there a better way to do this (put the parameter in the variable if
>> the parameter is not null, otherwise put 20) ?
>
> You can use the || (or) operator:
>
> David
but in this case if @params[:per_page] = 'blahblah' it works, I get 20
in per_page but if @params[:per_page] = 6 or another integer I get :
undefined method `>' for false:FalseClass
from Rails...
Nicolas.
unknown wrote:
···
On Mon, 28 Aug 2006, Nicolas Blanco wrote:
Thanks for the trick.
But the next problem is that if params[:per_page] equals 0 then per_page
equals 0 and I get the error message "must have at least one item per
page" from the Rails paginate option...
per_page = @params[:per_page] || 20
In that case, use the trinary operator. Call to_i on @params[:per_page]
if it could be nil, as you don't want to do that comparison with nil.
-----Original Message-----
From: Jan Svitok [mailto:jan.svitok@gmail.com]
Sent: Monday, August 28, 2006 4:01 PM
To: ruby-talk ML
Subject: Re: Newbie : assign a value to a variable...
On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:
> Thanks for the trick.
>
> But the next problem is that if params[:per_page] equals 0
then per_page
> equals 0 and I get the error message "must have at least
one item per
> page" from the Rails paginate option...