check_box with problem

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

<p>
<b>Mercados:</b><br /><br />
<%i=1%>
<% @mercado.each do |mercado| %>
<div>
<%= 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 %><div id="muestra_i"></div>
</div>
<%i=i+1%>
<% end %>
</p>

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]

any idea???

thanks

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 <div id="muestra_i"> (and not
<div id="muestra_1">, <div id="muestra_2">, ..._

Fred

<div id="muestra_i">

deberia ser

<div id="muestra<%= i %>">

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.

Another thing. instead of

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

end

you can do:

@collecation.each_with_index |obj, i|
...
end

Or use a partial.

Fred

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

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

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| %>
<div>
<%= 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 %><div id="muestra_i"></div>
</div>
<%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

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!