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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Aug 22, 2:42 pm, Maite Piedra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> <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>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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<div id="muestra_i"> deberia ser <div id="muestra<%= i %>"> On Aug 22, 2:45 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 22, 2:42 pm, Maite Piedra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > <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> > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 22, 2:42 pm, Maite Piedra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > <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> > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Aug 2008, at 15:08, Wolas! wrote:> > Another thing. instead of > > i = 0 > @collection.each do |obj| > obj += i > > end > > you can do: > > @collecation.each_with_index |obj, i| > ... > endOr use a partial. Fred> > > > same thing, more readable. > > On Aug 22, 2:45 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Aug 22, 2:42 pm, Maite Piedra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >> >>> <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> >> >> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 22 Aug 2008, at 15:08, Wolas! wrote:
>
>
>
> > 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
>
>
>
> > same thing, more readable.
>
> > On Aug 22, 2:45 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
> >> On Aug 22, 2:42 pm, Maite Piedra
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> >> wrote:
>
> >>> <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>
>
> >> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Aug 2008, at 19:01, Maite Piedra 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| %> <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> I use partial in mostrar2 > > render :update do |page| > page.replace_html ''muestra_'' + params[:id_valor], :partial => > ''muestra2'', :object => @empresa > > thanks again > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 22 Aug 2008, at 19:01, Maite Piedra 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| %>
> <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
>
> > I use partial in mostrar2
>
> > render :update do |page|
> > page.replace_html ''muestra_'' +
params[:id_valor], :partial =>
> > ''muestra2'', :object => @empresa
>
> > thanks again
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---