# Check\_box with problem

**URL:** <https://www.ruby-forum.com/t/check-box-with-problem/146577>\
**Category:** Rails\
**Created:** [August 22, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577 "2008-08-22T15:42:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/1 "2008-08-22T15:42:03Z")

</div>

hello list!!!

I have this in my proyect

in my controller  
def elegir  
@mercado=Mercado.find(:all)  
@empresas=Empresa.find(:first)  
respond\_to do |format|  
format.html {render :layout =\> false}  
end  
end

elegir view is

**Mercados:**  
  
\<%i=1%\> \<% @mercado.each do |mercado| %\>

\<%= check\_box\_tag "empresa[mercado\_ids][]", mercado.id, @empresas.mercados.include?(mercado), {:onclick =\> remote\_function(:url =\>{:action =\> "mostrar2", :id\_valor =\>i}, :with =\> "'id=#{mercado.id}'")} %\> \<%= mercado.nombre %\>

\<%i=i+1%\> \<% end %\>

I pass diferent muestra\_i for check\_box  
and mostrar2 is…

def mostrar2  
@empresa = []  
@mercado= Mercado.find(params[:id])  
@empresa \<\< @mercado.empresas  
render :update do |page|  
page.replace\_html ‘muestra\_’ + params[:id\_valor], :partial =\>  
‘muestra2’, :object =\> @empresa  
end  
end

but when I click on the check\_box, this is the messages

TypeError: element is null

Element.update(“muestra\_2”, “\n\n\u003Cbr /\u003E\n\u003Cp\u003ELas  
empresas de este mercado son \n\n \u003Cp\u003E\u003Cb\u003ENombre:  
\u003C/b\u003E acciona \u003C/p\u003E\n\u003C/p\u003E\n\n  
\u003Cp\u003E\u003Cb\u003ENombre: \u003C/b\u003E gtf  
\u003C/p\u003E\n\u003C/p\u003E\n\n\n”);

and my file development.log write this

Completed in 0.29700 (3 reqs/sec) | Rendering: 0.04700 (15%) | DB:  
0.09500 (31%) | 200 OK [[http://localhost/mercados/mostrar2?id\_valor=2](http://localhost/mercados/mostrar2?id_valor=2)]

any idea???

thanks

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/2 "2008-08-22T15:46:40Z")

</div>

On Aug 22, 2:42 pm, Maite P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> **Mercados:**  
>   
> \<%i=1%\> \<% @mercado.each do |mercado| %\>
> 
> \<%= check\_box\_tag "empresa[mercado\_ids][]", mercado.id, @empresas.mercados.include?(mercado), {:onclick =\> remote\_function(:url =\>{:action =\> "mostrar2", :id\_valor =\>i}, :with =\> "'id=#{mercado.id}'")} %\> \<%= mercado.nombre %\>

The error message is telling you that you don’t have an element with  
id muestra\_2. Have you checked the html you generate?  
I bet you’ll find that all your divs are

 (and not

, 
, ...\_

Fred

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/3 "2008-08-22T16:07:45Z")

</div>

deberia ser

On Aug 22, 2:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/4 "2008-08-22T16:08:55Z")

</div>

Another thing. instead of

i = 0  
@collection.each do |obj|  
obj += i

end

you can do:

@collecation.each\_with\_index |obj, i|  
…  
end

same thing, more readable.

On Aug 22, 2:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/5 "2008-08-22T16:12:26Z")

</div>

On 22 Aug 2008, at 15:08, Wolas! wrote:

> @collecation.each\_with\_index |obj, i|  
> …  
> end

Or use a partial.

Fred

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/6 "2008-08-22T16:45:53Z")

</div>

Defenitely use a partial if you can. they save readability,  
manageability and are just very cool ingeneral.

from docs:

# Renders the same partial with a local variable.

render :partial =\> “person”, :locals =\> { :name =\> “david” }

# Renders the partial, making @new\_person available through

# the local variable ‘person’

render :partial =\> “person”, :object =\> @new\_person

# Renders a collection of the same partial by making each element

# of @winners available through the local variable “person” as it

# builds the complete response.

render :partial =\> “person”, :collection =\> @winners

# Renders the same collection of partials, but also renders the

# person\_divider partial between each person partial.

render :partial =\> “person”, :collection =\>  
@winners, :spacer\_template =\> “person\_divider”

# Renders a collection of partials located in a view subfolder

# outside of our current controller. In this example we will be

# rendering app/views/shared/\_note.r(html|xml) Inside the partial

# each element of @new\_notes is available as the local var “note”.

render :partial =\> “shared/note”, :collection =\> @new\_notes

# Renders the partial with a status code of 500 (internal error).

render :partial =\> “broken”, :status =\> 500

On Aug 22, 3:11 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/7 "2008-08-22T20:01:34Z")

</div>

thanks for all, now it is ok  
but I donÂ´t undestand the last

> Defenitely use a partial if you can. they save readability,  
> manageability and are just very cool ingeneral.

use partial where I have my check\_box, in elegir.html.erb???

I use partial in mostrar2

```
render :update do |page|
    page.replace_html 'muestra_' + params[:id_valor], :partial =>

```

‘muestra2’, :object =\> @empresa

thanks again

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 22, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/8 "2008-08-22T22:09:41Z")

</div>

On 22 Aug 2008, at 19:01, Maite P. wrote:

> thanks for all, now it is ok  
> but I don´t undestand the last
> 
> > Defenitely use a partial if you can. they save readability,  
> > manageability and are just very cool ingeneral.
> 
> use partial where I have my check\_box, in elegir.html.erb???

No. the suggestion was replace

\<%i=1%\>  
\<% @mercado.each do |mercado| %\>

\<%= check\_box\_tag "empresa[mercado\_ids][]", mercado.id, @empresas.mercados.include?(mercado), {:onclick =\> remote\_function(:url =\>{:action =\> "mostrar2", :id\_valor =\>i}, :with =\> "'id=#{mercado.id}'")} %\> \<%= mercado.nombre %\>

\<%i=i+1%\> \<% end %\>

with

render :partial =\> ‘mercado’, :collection =\> @mercado

Inside the mercado partial there will be a mercado\_counter variable so  
you don’t need to mess around with that i variable

Fred

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [August 24, 2008, 2:05am UTC](https://www.ruby-forum.com/t/check-box-with-problem/146577/9 "2008-08-24T02:05:28Z")

</div>

Adding to Fred’s suggestion, you would want to do this:

controller:  
@mercados = Mercado.find :all

view:  
render :partial =\> @mercados

partial:  
\<% div\_for mercado %\>  
\<%= check\_box\_tag “empresa[mercado\_ids][]”, mercado.id,  
@empresas.mercados.include?(mercado), {:onclick =\>  
remote\_function(:url =\>{:action =\> “mostrar2”, :id\_valor=\>i}, :with =\>  
“‘id=#{mercado.id}’”)} %\>  
\<%= mercado.nombre %\>  
\<% end %\>

There are ways to clean this up more (for example, pass @empresas  
through :local in the render partial declaration) and I believe some  
of the functions I’ve used will only work this way under Rails 2+.  
div\_for will automatically create div id’s like ‘mercado\_3’, etc.

Hope this helps!

On Aug 22, 1:02 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
