Forum: Ruby on Rails params[:xxx]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fresh M. (Guest)
on 2009-01-10 20:34
If I have <select name="user[city]"> what is params[:???]

params[:user[city]] ???
Vishwanath N. (Guest)
on 2009-01-10 20:36
(Received via mailing list)
params[:user][:city]

NAYAK

On Sun, Jan 11, 2009 at 12:04 AM, Gi Ga
<removed_email_address@domain.invalid>wrote:

>
> If I have <select name="user[city]"> what is params[:???]
>
> params[:user[city]] ???
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
- NAYAK
Fresh M. (Guest)
on 2009-01-10 21:42
@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.
Phlip (Guest)
on 2009-01-10 21:52
(Received via mailing list)
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].
Fresh M. (Guest)
on 2009-01-10 22:11
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 (Guest)
on 2009-01-10 22:22
(Received via mailing list)
Try || not |
Fresh M. (Guest)
on 2009-01-10 22:29
cloper wrote:
> Try || not |

Same error :(
Vishwanath N. (Guest)
on 2009-01-10 23:29
(Received via mailing list)
@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 :(
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
- NAYAK
Jej (Guest)
on 2009-01-11 13:53
(Received via mailing list)
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
Fresh M. (Guest)
on 2009-01-11 14:00
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?
MaD (Guest)
on 2009-01-11 14:07
(Received via mailing list)
> @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.
Sazima (Guest)
on 2009-01-11 14:40
(Received via mailing list)
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
Phlip (Guest)
on 2009-01-11 17:28
(Received via mailing list)
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...
This topic is locked and can not be replied to.