# Updates using urls and query strings

**URL:** <https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570>\
**Category:** Rails\
**Created:** [December 13, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570 "2008-12-13T18:23:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570/1 "2008-12-13T18:23:59Z")

</div>

I have a task model and in the controller there is this code that sets  
the date a task was completed on if params[:completed] comes back as  
true and sets the completed\_by field to nil (ie incomplete) if params  
[:completed] comes back as false:

@task.completed\_on = params[:completed] ? Time.now.to\_s(:db) : nil

Users can edit the task and this code is in the edit view - allowing  
the user to tick the box to mark a task as complete or untick it to  
change the task’s status back to incomplete:  
\<%= check\_box\_tag :completed, “1”, @task.completed\_on %\>

This all works fine. However, if I try to use AJAX to access the  
update controller directly with the following urls:

/tasks/3?completed=true, \_method = PUT  
/tasks/3?completed=false, \_method = PUT

The first one works, but the second doesn’t reset the completed\_by  
field back to nil.

Does anybody know why this isn’t working? I’ve a feeling it might be  
because ruby is seeing ‘false’ as a string and therefore ‘true’.

cheers,

DAZ

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570/2 "2008-12-13T18:29:15Z")

</div>

> Does anybody know why this isn’t working? I’ve a feeling it might be  
> because ruby is seeing ‘false’ as a string and therefore ‘true’.

Precisely. all parameters are just strings, it’s up to you to coerce  
it to a boolean value

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570/3 "2008-12-13T20:35:24Z")

</div>

Thanks for the reply Frederick, I just have two questions:

1. How exactly do I coerce it into a boolean value - is there a best-  
practice way of doing this?

2. How come this problem doesn’t occur when the value is submitted via  
the form from the edit action?

cheers,

DAZ

On Dec 13, 5:28 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 14, 2008, 12:13am UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570/4 "2008-12-14T00:13:17Z")

</div>

On Dec 13, 7:32 pm, DAZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply Frederick, I just have two questions:
> 
> 1. How exactly do I coerce it into a boolean value - is there a best-  
> practice way of doing this?

doesn’t need to be any more complicated than if params[:foo] ==  
‘true’ …

> 1. How come this problem doesn’t occur when the value is submitted via  
> the form from the edit action?

if you were just using update\_attributes then it understands this sort  
of stuff IIRC.

Fred

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 14, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/updates-using-urls-and-query-strings/155570/5 "2008-12-14T19:11:22Z")

</div>

Cheers Fred, that worked great!

DAZ

On Dec 13, 11:12 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
