maite
August 22, 2008, 3:42pm
1
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 ]
any idea???
thanks
maite
August 22, 2008, 3:46pm
2
On Aug 22, 2:42 pm, Maite P. [email protected]
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
maite
August 22, 2008, 4:08pm
4
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. [email protected]
maite
August 22, 2008, 4:12pm
5
On 22 Aug 2008, at 15:08, Wolas! wrote:
@collecation.each_with_index |obj, i|
…
end
Or use a partial.
Fred
maite
August 22, 2008, 4:45pm
6
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. [email protected]
maite
August 22, 2008, 8:01pm
7
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
maite
August 22, 2008, 10:09pm
8
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
maite
August 24, 2008, 2:05am
9
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. [email protected]