Params[:xxx]

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

params[:user[city]] ???

params[:user][:city]

NAYAK

On Sun, Jan 11, 2009 at 12:04 AM, Gi Ga
[email protected]wrote:

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

params[:user[city]] ???

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

  • NAYAK

@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.

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].

Try || not |

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.

cloper wrote:

Try || not |

Same error :frowning:

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

@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. <
[email protected]> wrote:

cloper wrote:

Try || not |

Same error :frowning:

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

  • NAYAK

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?

@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.

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

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…