Params[:xxx]


#1

If I have what is params[:???]

params[:user[city]] ???


#2

params[:user][:city]

NAYAK

On Sun, Jan 11, 2009 at 12:04 AM, Gi Ga
removed_email_address@domain.invalidwrote:

If I have what is params[:???]

params[:user[city]] ???

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

  • NAYAK

#3

@city = params[:user][:city]

Why I get:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Even if I try:

@city = params[:user][:city] if params[:user][:city]

I get same error.


#4

James B. wrote:

@city = params[:user][:city] if params[:user][:city]
@city = params[:user][:city] if params[:user]

nil propagation indeed sucks, but if you have no [:user], you can’t have
a
[:user][:city].


#5

Try || not |


#6

Phlip wrote:

James B. wrote:

@city = params[:user][:city] if params[:user][:city]
@city = params[:user][:city] if params[:user]

nil propagation indeed sucks, but if you have no [:user], you can’t have
a
[:user][:city].

Why this doesn’t work:

@city = params[:user][:city] | “xxx”

Same error.


#7

cloper wrote:

Try || not |

Same error :frowning:


#8

Look at your server’s log, you can find the parameters for each
request.

eg.

Processing XxxController#update (for 127.0.0.1 at 2009-01-10 14:44:03)
[GET]
Session ID: 4598628b1689321ec39c1b22fb0e7728
Parameters: {“param1”=>"…", “param2”=>"…", …}

Jej


#9

@city = params[:user][:city] if params[:user] &&
params[:user][:city]Should
do the trick

NAYAK

On Sun, Jan 11, 2009 at 1:59 AM, James B. <
removed_email_address@domain.invalid> wrote:

cloper wrote:

Try || not |

Same error :frowning:

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

  • NAYAK

#10

Vishwanath Nayak wrote:

@city = params[:user][:city] if params[:user] &&
params[:user][:city]Should
do the trick

Yes it do, but it not simple any more, as it was before.

For example: This works in old rails versions, but not in (2.2.2), why?

@word = params[:search][:word] || params[:word] || “home”

The are 2 parameters, one POST and another GET (POST overwrite GET), if
there not POST and GET, defaul value is: “home”

How can I do it in Rials 2.2.2?


#11

@word = params[:search][:word] || params[:word] || “home”
you can still do that. there is only a problem if params[:search] is
nil. because then you’d try to evaluate nil.[] which again leads to an
error. this is what Phlip wanted to tell you way up in this thread.


#12

MaD is right, no matter what the expression, you will always get an
exception if you try to evaluate nil.[]

You can divide the expression in two:

city = (params[:user]) ? params[:user][:city] || “xxx” : “xxx”

Cheers, Sazima


#13

MaD wrote:

@word = params[:search][:word] || params[:word] || “home”

you can still do that. there is only a problem if params[:search] is
nil. because then you’d try to evaluate nil.[] which again leads to an
error. this is what Phlip wanted to tell you way up in this thread.

And if James B.'s posts still don’t reflect that awareness, I did not
want to
get into…

@word = params.fetch(:search, params).fetch(:word, ‘home’)

Another fix - both the GET and POST flavors of this action should have
the same
params, meaning the both things which hit the action from a web browser
should
put their goodies into :search=>{} block. That’s a best practice…