# What is the \[phone\] representing?

**URL:** <https://www.ruby-forum.com/t/what-is-the-phone-representing/149349>\
**Category:** Rails\
**Created:** [September 23, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349 "2008-09-23T22:17:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 23, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/1 "2008-09-23T22:17:28Z")

</div>

Hey all,

trying to get a concept down. Or maybe the syntax.

Here is the code:

Phones

\<% @person.phones.each { |phone| %\> \<%= tag("input", "type" =\> "text", "id" =\> "phones[#{phone.id}][phone]", "name" =\> "phones[#{phone.id}][phone]", "value" =\> phone.phone) %\>

\<%= phone.errors.on “phone” %\>  
  
\<% } %\>

Add new phone: (More phones can be added later)

What, exactly, is the [phone] in phones[#{phone.id][phone]? I know  
phones is the db phones. I know phone id is the PK (and for  
phones[new][phone]. What is phone though? Also, in phones[new][phone],  
what is new creating/doing?

Similarly:

def update  
@person = Person.find(@params[“person”][“id”])

```
 @person.attributes = @params["person"]

 if @person.save
   redirect_to :action => "show", :id => @person.id
 else
   render "person/edit"

 end

```

end

What is [“person”]? What is it referencing?

Thanks.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 23, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/2 "2008-09-23T22:27:01Z")

</div>

On 23 Sep 2008, at 21:17, Dave L. wrote:

> What, exactly, is the [phone] in phones[#{phone.id][phone]? I know  
> phones is the db phones. I know phone id is the PK (and for  
> phones[new][phone]. What is phone though? Also, in phones[new]  
> [phone],  
> what is new creating/doing?

This is Rails parameter naming converion

phones[#{phone.id}][phone] means that the corresponding input element  
will (in the controller) be at params[‘phones’][‘25’][‘phone’]  
(assuming the id was 25).

The ‘new’ is presumably just a convention by whoever wrote this code  
to indicate a new record.

I wrote some stuff up about this:

> **[Parametrised to the max - Space Vatican](https://www.spacevatican.org/2008/7/18/parametrised-to-the-max/)**
>
> If you’ve played with Rails for more that about 3 minutes you’ll know that the params hash can itself contain hashes. If you use form\_for …

> ```
> render "person/edit"
> 
> end
> 
> ```
> 
> end

> What is [“person”]? What is it referencing?

Not sure what you’re asking about. params is just a regular hash,  
they’re pulling out the value with key “person”

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 23, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/3 "2008-09-23T22:50:34Z")

</div>

Ah, so basically foo[#{a}][b] is nothing more but Ruby conventional  
syntax for  
@params[‘foo’]['#{a}][b]

As for my other question…I think I understand it now, but I want to be  
sure and clarify something.

def update  
@person = Person.find(@params[“person”][“id”])

```
 @person.attributes = @params["person"]

 if @person.save
   redirect_to :action => "show", :id => @person.id
 else
   render "person/edit"

 end

```

end

```
 @person = Person.find(@params["person"]["id"])

```

@params[“person”][“id”] could be person[#{id}]. Of course, Person.find  
is used because it will return a person record with the corresponding  
id.

I am assuming that since Ruby can go from singular to plural, it is  
smart enough to know that People.find wants a single record that can  
only be found in the people db (as People represents multiple persons).

Frederick C. wrote:

> On 23 Sep 2008, at 21:17, Dave L. wrote:
> 
> > What, exactly, is the [phone] in phones[#{phone.id][phone]? I know  
> > phones is the db phones. I know phone id is the PK (and for  
> > phones[new][phone]. What is phone though? Also, in phones[new]  
> > [phone],  
> > what is new creating/doing?
> 
> This is Rails parameter naming converion
> 
> phones[#{phone.id}][phone] means that the corresponding input element  
> will (in the controller) be at params[‘phones’][‘25’][‘phone’]  
> (assuming the id was 25).
> 
> The ‘new’ is presumably just a convention by whoever wrote this code  
> to indicate a new record.
> 
> I wrote some stuff up about this:  
> [Parametrised to the max - Space Vatican](http://www.spacevatican.org/2008/7/18/parametrised-to-the-max)
> 
> > ```
> > render "person/edit"
> > 
> > end
> > 
> > ```
> > 
> > end
> 
> > What is [“person”]? What is it referencing?
> 
> Not sure what you’re asking about. params is just a regular hash,  
> they’re pulling out the value with key “person”

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 23, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/4 "2008-09-23T22:58:07Z")

</div>

On 23 Sep 2008, at 21:50, Dave L. wrote:

> Ah, so basically foo[#{a}][b] is nothing more but Ruby conventional  
> syntax for  
> @params[‘foo’][’#{a}][b]

sort of. it’s the parameter name that will make the parameters arrive  
in that way.

> I am assuming that since Ruby can go from singular to plural, it is  
> smart enough to know that People.find wants a single record that can  
> only be found in the people db (as People represents multiple  
> persons).

Yes - rails knows that the table backing the Person class is people

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 24, 2008, 8:31am UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/5 "2008-09-24T08:31:39Z")

</div>

Sent from my iPhone

On 24 Sep 2008, at 00:30, Dave L.  
\<removed\_email\_address@domain.invalid

> wrote:

> > sort of. it’s the parameter name that will make the parameters arrive  
> > in that way.
> 
> Why does the param’s name allow it to arrive in that way?

Because that’s what the bit of code parsins query strings/form data is  
looking for

Fred

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [September 24, 2008, 1:30am UTC](https://www.ruby-forum.com/t/what-is-the-phone-representing/149349/6 "2008-09-24T01:30:19Z")

</div>

> sort of. it’s the parameter name that will make the parameters arrive  
> in that way.

Why does the param’s name allow it to arrive in that way?
