# Syntax for observe\_field( :with =\>

**URL:** <https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117>\
**Category:** Rails\
**Created:** [June 26, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117 "2006-06-26T15:49:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 26, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117/1 "2006-06-26T15:49:13Z")

</div>

Greetings!

What is up with the syntax of this thing? I mean, if the only thing I  
can send back using :with is the field being observed, then why in the  
world is the syntax so convoluted? For example, what I’ve been able to  
get working is:

\<%= text\_field\_tag(‘date’, illness\_date, :size =\> 30) %\>

  
\<%= observe\_field(‘date’, :url =\> {:action =\> ‘update\_date’, :id =\>  
illness\_id}, :with =\> “‘idate=’+$(‘date’).value” ) %\>

Of course, maybe I’m just doing it wrong. I suppose it might make some  
sense if it’s possible to send more than one params value back with the  
:with option. Does anybody know anything about how that might be  
possible? I’ve spent a day and a half trying to figure it out and  
haven’t been able to get anything but the above to work.

TIA,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 26, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117/2 "2006-06-26T16:01:36Z")

</div>

Hi Bill,

You can send anything back to your script. The syntax is pure  
javascript  
that is eval’d. You can use it to build a custom parameter string. I’m  
not  
sure if it needs to be escaped to be url encoded or not.

:with =\> %q{“idate=” + $(‘date’).value + “&some\_other\_value=” +  
$(‘some\_other\_field’).value}

Hope this is strictly correct. I’m sure someone will correct me  
otherwise  
🙂 at least I hope so.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 26, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117/3 "2006-06-26T17:09:04Z")

</div>

Hi Daniel,

Thanks. I haven’t tried the %q syntax (although that’s about the _only_  
thing I haven’t tried!). I’ll give it a shot. I did find something on  
the wiki just after I sent my original post that implies you can only  
send back one params value _but_ you can put a bunch of stuff into the  
one string you’ll get back and then parse out the pieces. I definitely  
hope your approach works, but at least it looks like there’s a way to do  
what I need, even if it’s coyote-ugly 😉

Thanks again,  
Bill  
----- Original Message -----  
From: Daniel N  
To: removed\_email\_address@domain.invalid  
Sent: Monday, June 26, 2006 8:57 AM  
Subject: [Norton AntiSpam] Re: [Rails] syntax for observe\_field( :with  
=\>

Hi Bill,

You can send anything back to your script. The syntax is pure  
javascript that is eval’d. You can use it to build a custom parameter  
string. I’m not sure if it needs to be escaped to be url encoded or  
not.

:with =\> %q{“idate=” + $(‘date’).value + “&some\_other\_value=” +  
$(‘some\_other\_field’).value}

Hope this is strictly correct. I’m sure someone will correct me  
otherwise 🙂 at least I hope so.

On 6/26/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Greetings!

```
What is up with the syntax of this thing? I mean, if the only thing 

```

I can send back using :with is the field being observed, then why in the  
world is the syntax so convoluted? For example, what I’ve been able to  
get working is:

```
<%= text_field_tag('date', illness_date, :size => 30) %></p>
<%= observe_field('date', :url => {:action => 'update_date', :id => 

```

illness\_id}, :with =\> “‘idate=’+$(‘date’).value” ) %\>

```
Of course, maybe I'm just doing it wrong. I suppose it might make 

```

some sense if it’s possible to send more than one params value back with  
the :with option. Does anybody know anything about how that might be  
possible? I’ve spent a day and a half trying to figure it out and  
haven’t been able to get anything but the above to work.

```
TIA,
Bill

_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

```

* * *

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 26, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/syntax-for-observe-field-with/96117/4 "2006-06-26T17:49:36Z")

</div>

On 6/27/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Yeah, it is very ugly. I guess you could wrap it up into a helper.  
There’s  
no doubt a much better way to do this.

def with\_fields\_for\_observe( \*fields )  
fields.flatten!  
out = “”  
join = “”  
fields.each do |field|  
out \<\< join \<\< “‘#{field.to\_s}=’ + $(‘#{field.to\_s}’).value”  
join = " + ‘&’ + "  
end  
out  
end

Then you could call the with parameter with  
:with =\> with\_fields\_for\_observe( :a\_field\_id, ‘some\_other\_id’,  
[:yet\_another\_id, :last\_one] )

The access in your controller should be similar to a normal parameter  
string. Best to check the output tho.

Hope this minimises the ugly to a reasonable level. 😉

Thanks again,
